swift ios 在上传前减小图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43256005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift ios reduce image size before upload
提问by user2722667
I am trying to reduce the file size of my images as much as possible before I upload them. Right now I am doing this:
在上传图像之前,我试图尽可能地减小图像的文件大小。现在我正在这样做:
if let firstImageData = UIImageJPEGRepresentation(pickedImage, 0.1) {
self.imgArray.append(firstImageData)
}
This will take any image coming from the camera or photo album, make it jpg and reduce its size.
这将拍摄来自相机或相册的任何图像,使其成为 jpg 并缩小其尺寸。
I have set the setting to 0.1but when I upload my images the size still end up around 300-350kb, is there any way I could resize them even more, I am aiming towards 50-70kb
我已将设置设置为0.1,但是当我上传我的图像时,大小仍然在 300-350kb 左右,有什么方法可以调整它们的大小,我的目标是 50-70kb
回答by Jagdeep
you can resize your image first to smaller sizeusing these extension by resizing it by percent or width
您可以使用这些扩展程序通过按百分比或宽度调整大小来首先将图像大小调整为较小的大小
extension UIImage {
func resizeWithPercent(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
}
}
to use it just call it like this
使用它只是这样称呼它
myImage = myImage.resizeWithWidth(700)!
Now next you can still compress it using compression ratio of your choice
现在接下来您仍然可以使用您选择的压缩率来压缩它
let compressData = UIImageJPEGRepresentation(myImage, 0.5) //max value is 1.0 and minimum is 0.0
let compressedImage = UIImage(data: compressData!)
回答by Robert Juz
You only can change size of image (less size = less data) and compress it by using image compression algorithms like JPEG, there is no other way (better algorythm = less size in same quality).
您只能更改图像的大小(较小的尺寸 = 较少的数据)并使用 JPEG 等图像压缩算法对其进行压缩,没有其他方法(更好的算法 = 相同质量的较小尺寸)。
I heard google improved JPEG algorithm using neural networks lately (Google's TensorFlow)
我最近听说谷歌使用神经网络改进了 JPEG 算法(谷歌的 TensorFlow)