8

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.

I have set the setting to 0.1 but 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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2722667
  • 8,195
  • 14
  • 52
  • 100

2 Answers2

39

you can resize your image first to smaller size using 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!)
Mr Stanev
  • 1,662
  • 1
  • 19
  • 26
Jagdeep
  • 1,158
  • 11
  • 16
  • 1
    This seems to be working, BUT for some reason, it makes the images look hideous. Super blurry. Any idea why this might be happening and a workaround? – tapizquent Mar 05 '19 at 04:58
  • Increase the compression value to 0.75 or 0.8 instead of 0.5 – Ansyar Hafid Jan 23 '20 at 15:48
  • https://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size, see Leo's answer. – Ning May 05 '20 at 05:40
3

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).

I heard google improved JPEG algorithm using neural networks lately (Google’s TensorFlow)

Robert Juz
  • 126
  • 7