如何调整UIImage的大小以减less上传图片的大小

我一直在search谷歌,只有遇到的库降低高度/宽度或一些如何通过CoreImage编辑UIImage外观。 但我还没有看到或find一个图书馆,解释如何减less图片的大小,所以上传时,这不是完整的图像大小。

到目前为止,我有这样的:

if image != nil { //let data = NSData(data: UIImagePNGRepresentation(image)) let data = UIImagePNGRepresentation(image) body.appendString("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"image\"; filename=\"randomName\"\r\n") body.appendString("Content-Type: image/png\r\n\r\n") body.appendData(data) body.appendString("\r\n") } 

它发送12MB的照片。 我怎样才能减less到1MB? 谢谢!

Xcode 8.2.1•Swift 3.0.2

 extension UIImage { func resized(withPercentage percentage: CGFloat) -> UIImage? { let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage) UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale) defer { UIGraphicsEndImageContext() } draw(in: CGRect(origin: .zero, size: canvasSize)) return UIGraphicsGetImageFromCurrentImageContext() } func resized(toWidth width: CGFloat) -> UIImage? { let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height))) UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale) defer { UIGraphicsEndImageContext() } draw(in: CGRect(origin: .zero, size: canvasSize)) return UIGraphicsGetImageFromCurrentImageContext() } } 

用法:

 let myPicture = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))! let myThumb1 = myPicture.resized(withPercentage: 0.1) let myThumb2 = myPicture.resized(toWidth: 72.0) 

这是我遵循的方式来调整图像大小。

  -(UIImage *)resizeImage:(UIImage *)image { float actualHeight = image.size.height; float actualWidth = image.size.width; float maxHeight = 300.0; float maxWidth = 400.0; float imgRatio = actualWidth/actualHeight; float maxRatio = maxWidth/maxHeight; float compressionQuality = 0.5;//50 percent compression if (actualHeight > maxHeight || actualWidth > maxWidth) { if(imgRatio < maxRatio) { //adjust width according to maxHeight imgRatio = maxHeight / actualHeight; actualWidth = imgRatio * actualWidth; actualHeight = maxHeight; } else if(imgRatio > maxRatio) { //adjust height according to maxWidth imgRatio = maxWidth / actualWidth; actualHeight = imgRatio * actualHeight; actualWidth = maxWidth; } else { actualHeight = maxHeight; actualWidth = maxWidth; } } CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); UIGraphicsBeginImageContext(rect.size); [image drawInRect:rect]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality); UIGraphicsEndImageContext(); return [UIImage imageWithData:imageData]; } 

使用这种方法我的图像有6.5 MB减less到104 KB。

与“狮子座答案”相同,但SWIFT 2.0的编辑很less

  extension UIImage { func resizeWithPercentage(percentage: CGFloat) -> UIImage? { let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage))) imageView.contentMode = .ScaleAspectFit imageView.image = self UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale) guard let context = UIGraphicsGetCurrentContext() else { return nil } imageView.layer.renderInContext(context) guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil } UIGraphicsEndImageContext() return result } func resizeWithWidth(width: CGFloat) -> UIImage? { let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height))))) imageView.contentMode = .ScaleAspectFit imageView.image = self UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale) guard let context = UIGraphicsGetCurrentContext() else { return nil } imageView.layer.renderInContext(context) guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil } UIGraphicsEndImageContext() return result } } 

如果有人正在寻找resize的图像小于1MBSwift 3和4

 extension UIImage { func resized(withPercentage percentage: CGFloat) -> UIImage? { let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage) UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale) defer { UIGraphicsEndImageContext() } draw(in: CGRect(origin: .zero, size: canvasSize)) return UIGraphicsGetImageFromCurrentImageContext() } func resizedTo1MB() -> UIImage? { guard let imageData = UIImagePNGRepresentation(self) else { return nil } var resizingImage = self var imageSizeKB = Double(imageData.count) / 1024.0 while imageSizeKB > 1024 { guard let resizedImage = resizingImage.resized(withPercentage: 0.9), let imageData = UIImagePNGRepresentation(resizedImage) else { return nil } resizingImage = resizedImage imageSizeKB = Double(imageData.count) / 1024.0 } return resizingImage } } 

用法:

 let resizedImage = originalImage.resizedTo1MB() 

这里是user4261201的答案,但在迅速,我目前正在使用:

 func compressImage (_ image: UIImage) -> UIImage { let actualHeight:CGFloat = image.size.height let actualWidth:CGFloat = image.size.width let imgRatio:CGFloat = actualWidth/actualHeight let maxWidth:CGFloat = 1024.0 let resizedHeight:CGFloat = maxWidth/imgRatio let compressionQuality:CGFloat = 0.5 let rect:CGRect = CGRect(x: 0, y: 0, width: maxWidth, height: resizedHeight) UIGraphicsBeginImageContext(rect.size) image.draw(in: rect) let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()! let imageData:Data = UIImageJPEGRepresentation(img, compressionQuality)! UIGraphicsEndImageContext() return UIImage(data: imageData)! } 

如果您以NSData格式上传图像,请使用以下命令:

 NSData *imageData = UIImageJPEGRepresentation(yourImage, floatValue); 

yourImage是你的UIImagefloatvalue是压缩值(0.0到1.0)

以上是将图像转换为JPEG

对于PNG使用: UIImagePNGRepresentation

注意:以上代码在Objective-C中。 请检查如何在Swift中定义NSData。

使用.resizeToMaximumBytes调整UIImage的大小

这就是我在Swift 3中做的调整UIImage大小的方法。 它将图像大小缩小到100kb以内。 它按比例工作!

 extension UIImage { class func scaleImageWithDivisor(img: UIImage, divisor: CGFloat) -> UIImage { let size = CGSize(width: img.size.width/divisor, height: img.size.height/divisor) UIGraphicsBeginImageContext(size) img.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) let scaledImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return scaledImage! } } 

用法:

 let scaledImage = UIImage.scaleImageWithDivisor(img: capturedImage!, divisor: 3)