如何调整UIImage的大小以减少上传图像大小

我一直在搜索谷歌,并且只会遇到减少高度/宽度的库或者通过CoreImage编辑UIImage外观的库。但我没有看到或找到一个库,帖子解释了如何减少图像大小,所以当它上传时,它不是完整的图像大小。


到目前为止我有这个:


        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的照片。我怎样才能减少到1mb?谢谢!


PIPIONE
浏览 667回答 3
3回答

临摹微笑

这是我按照调整图像大小的方式。&nbsp;-(UIImage *)resizeImage:(UIImage *)image{&nbsp; &nbsp;float actualHeight = image.size.height;&nbsp; &nbsp;float actualWidth = image.size.width;&nbsp; &nbsp;float maxHeight = 300.0;&nbsp; &nbsp;float maxWidth = 400.0;&nbsp; &nbsp;float imgRatio = actualWidth/actualHeight;&nbsp; &nbsp;float maxRatio = maxWidth/maxHeight;&nbsp; &nbsp;float compressionQuality = 0.5;//50 percent compression&nbsp; &nbsp;if (actualHeight > maxHeight || actualWidth > maxWidth)&nbsp; &nbsp;{&nbsp; &nbsp; if(imgRatio < maxRatio)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //adjust width according to maxHeight&nbsp; &nbsp; &nbsp; &nbsp; imgRatio = maxHeight / actualHeight;&nbsp; &nbsp; &nbsp; &nbsp; actualWidth = imgRatio * actualWidth;&nbsp; &nbsp; &nbsp; &nbsp; actualHeight = maxHeight;&nbsp; &nbsp; }&nbsp; &nbsp; else if(imgRatio > maxRatio)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //adjust height according to maxWidth&nbsp; &nbsp; &nbsp; &nbsp; imgRatio = maxWidth / actualWidth;&nbsp; &nbsp; &nbsp; &nbsp; actualHeight = imgRatio * actualHeight;&nbsp; &nbsp; &nbsp; &nbsp; actualWidth = maxWidth;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; actualHeight = maxHeight;&nbsp; &nbsp; &nbsp; &nbsp; actualWidth = maxWidth;&nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);&nbsp; &nbsp;UIGraphicsBeginImageContext(rect.size);&nbsp; &nbsp;[image drawInRect:rect];&nbsp; &nbsp;UIImage *img = UIGraphicsGetImageFromCurrentImageContext();&nbsp; &nbsp;NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);&nbsp; &nbsp;UIGraphicsEndImageContext();&nbsp; &nbsp;return [UIImage imageWithData:imageData];}使用此方法,我的6.5 MB的图像减少到104 KB。Swift 4代码:func resize(_ image: UIImage) -> UIImage {&nbsp; &nbsp; var actualHeight = Float(image.size.height)&nbsp; &nbsp; var actualWidth = Float(image.size.width)&nbsp; &nbsp; let maxHeight: Float = 300.0&nbsp; &nbsp; let maxWidth: Float = 400.0&nbsp; &nbsp; var imgRatio: Float = actualWidth / actualHeight&nbsp; &nbsp; let maxRatio: Float = maxWidth / maxHeight&nbsp; &nbsp; let compressionQuality: Float = 0.5&nbsp; &nbsp; //50 percent compression&nbsp; &nbsp; if actualHeight > maxHeight || actualWidth > maxWidth {&nbsp; &nbsp; &nbsp; &nbsp; if imgRatio < maxRatio {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //adjust width according to maxHeight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgRatio = maxHeight / actualHeight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualWidth = imgRatio * actualWidth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualHeight = maxHeight&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if imgRatio > maxRatio {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //adjust height according to maxWidth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgRatio = maxWidth / actualWidth&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualHeight = imgRatio * actualHeight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualWidth = maxWidth&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualHeight = maxHeight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actualWidth = maxWidth&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))&nbsp; &nbsp; UIGraphicsBeginImageContext(rect.size)&nbsp; &nbsp; image.draw(in: rect)&nbsp; &nbsp; let img = UIGraphicsGetImageFromCurrentImageContext()&nbsp; &nbsp; let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality))&nbsp;&nbsp; &nbsp; UIGraphicsEndImageContext()&nbsp; &nbsp; return UIImage(data: imageData!) ?? UIImage()}

拉莫斯之舞

如果有人想要使用Swift 3和4将图像大小调整为小于1MB。只需复制并粘贴此扩展程序:extension UIImage {func resized(withPercentage percentage: CGFloat) -> UIImage? {&nbsp; &nbsp; let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage)&nbsp; &nbsp; UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)&nbsp; &nbsp; defer { UIGraphicsEndImageContext() }&nbsp; &nbsp; draw(in: CGRect(origin: .zero, size: canvasSize))&nbsp; &nbsp; return UIGraphicsGetImageFromCurrentImageContext()}func resizedTo1MB() -> UIImage? {&nbsp; &nbsp; guard let imageData = UIImagePNGRepresentation(self) else { return nil }&nbsp; &nbsp; var resizingImage = self&nbsp; &nbsp; var imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB&nbsp; &nbsp; while imageSizeKB > 1000 { // ! Or use 1024 if you need KB but not kB&nbsp; &nbsp; &nbsp; &nbsp; guard let resizedImage = resizingImage.resized(withPercentage: 0.9),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let imageData = UIImagePNGRepresentation(resizedImage)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { return nil }&nbsp; &nbsp; &nbsp; &nbsp; resizingImage = resizedImage&nbsp; &nbsp; &nbsp; &nbsp; imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB&nbsp; &nbsp; }&nbsp; &nbsp; return resizingImage}}并使用:let resizedImage = originalImage.resizedTo1MB()编辑: 请注意它是阻止UI,所以如果您认为这是适合您案例的正确方法,请转到后台主题。
打开App,查看更多内容
随时随地看视频慕课网APP