Last active
September 6, 2017 09:59
-
-
Save K-Kevin/f8d1a6d16993bd64c933a41f3bc0d0c1 to your computer and use it in GitHub Desktop.
swift comfress image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 图片压缩 | |
/// | |
/// - Parameters: | |
/// - maxFileSize: 压缩后的最大容量,单位 MB | |
/// - Returns: | |
func mcCompress(maxFileSize: Int) -> Data? { | |
let maxSize:CGFloat = 4320//4320X2880 | |
let width = self.size.width | |
let height = self.size.height | |
var newWidth = width | |
var newHeight = height | |
var newImage:UIImage? = self | |
print("Image size is: \(width)" + "x" + "\(height)") | |
//check if need Resize the image | |
if width > maxSize || height > maxSize { | |
if width > height { | |
newWidth = maxSize | |
newHeight = (height * maxSize) / width | |
} else { | |
newHeight = maxSize | |
newWidth = (width * maxSize) / height | |
} | |
// Resize the image | |
let newSize = CGSize(width: newWidth, height: newHeight) | |
UIGraphicsBeginImageContext(newSize) | |
self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) | |
newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
} | |
if let data = UIImageJPEGRepresentation(newImage!, 1) { | |
let bcf = ByteCountFormatter() | |
bcf.allowedUnits = [.useMB] | |
bcf.countStyle = .file | |
let string = bcf.string(fromByteCount: Int64(data.count)) | |
print("Data size is: \(string)") | |
if data.count > (maxFileSize * 1024 * 1024) { | |
let compression = CGFloat(maxFileSize * 1024 * 1024)/CGFloat(data.count) | |
if let newData = UIImageJPEGRepresentation(newImage!, compression) { | |
let bcf = ByteCountFormatter() | |
bcf.allowedUnits = [.useMB] | |
bcf.countStyle = .file | |
let string = bcf.string(fromByteCount: Int64(newData.count)) | |
print("newData size is: \(string)") | |
return newData | |
} | |
return nil | |
} | |
return data | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImage { | |
/// 图片压缩 | |
/// | |
/// - Parameters: | |
/// - maxFileSize: 压缩后的最大容量,单位 MB | |
/// - compression: 初始压缩值,默认为 1 | |
/// - Returns: | |
func mcCompress(maxFileSize: Int, compression: CGFloat = 1.0) -> Data? { | |
if let data = UIImageJPEGRepresentation(self, compression) { | |
let bcf = ByteCountFormatter() | |
bcf.allowedUnits = [.useMB] | |
bcf.countStyle = .file | |
let string = bcf.string(fromByteCount: Int64(data.count)) | |
print("Data size is: \(string)") | |
if data.count > (maxFileSize * 1024 * 1024) { | |
let newCompression = compression - 0.1 | |
let compressedData = self.mcCompress(maxFileSize: maxFileSize, compression: newCompression) | |
return compressedData | |
} | |
return data | |
} | |
return nil | |
} | |
func compress(image: UIImage, maxFileSize: Int, compression: CGFloat = 1.0, maxCompression: CGFloat = 0.4) -> Data? { | |
if let data = UIImageJPEGRepresentation(image, compression) { | |
let bcf = ByteCountFormatter() | |
bcf.allowedUnits = [.useMB] // optional: restricts the units to MB only | |
bcf.countStyle = .file | |
let string = bcf.string(fromByteCount: Int64(data.count)) | |
print("Data size is: \(string)") | |
if data.count > (maxFileSize * 1024 * 1024) && (compression > maxCompression) { | |
let newCompression = compression - 0.1 | |
let compressedData = self.compress(image: image, maxFileSize: maxFileSize, compression: newCompression, maxCompression: maxCompression) | |
return compressedData | |
} | |
return data | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment