3

I am currently using the following code to produce a fixed-size JPEG image from a UIImage object:

var ratio: CGFloat = 1
var imageData : Data = UIImageJPEGRepresentation(edited, ratio)!

while imageData.count > 200000 {
    if ratio == 0.1 {
        break
    }

    ratio = ratio - 0.1
    imageData = UIImageJPEGRepresentation(edited, ratio)!
}

However, this code seems a little inefficient as I need to continuously decrease the ratio until the image size is under the specified value (200 kb). Is there some better way to achieve this behavior in swift?

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29

2 Answers2

1

Use this extension in order to size down an image.

extension UIImage {

    func resizeImage(factor: CGFloat) -> UIImage? {
        let newWidth = self.size.width * factor    // Calculate size from factor.
        let newHeight = self.size.height * factor

        let size = CGSize(width: newWidth, height: newHeight)
        return resizeImage(targetSize: size)       // Return image with scaled factor.
    }

    func resizeImage(targetSize: CGSize) -> UIImage? {
        let originalSize = self.size

        let widthRatio = targetSize.width / originalSize.width
        let heightRatio = targetSize.height / originalSize.height
        let ratio = min(widthRatio, heightRatio)

        let newSize = CGSize(width: originalSize.width * ratio, height: originalSize.height * ratio)
        let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

        UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)
        self.draw(in: rect)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }

}

Using this, we can use your code above to achieve what you are trying to do.

var imageData: Data = UIImageJPEGRepresentation(edited, 1.0)

while imageData.count < 200000 {
     imageData = UIImageJPEGRepresentation(UIImage(data: imageData)!.resizeImage(factor: 0.9)!, 1.0)
     // imageData will be incremented down each time by a factor of 0.1 (1.0 - ratio)
}

EDIT: If you don't want to include an extension, you can just use imageData = UIImageJPEGRepresentation(imageData, 0.9) since it will still be scaling down by a factor of 0.1 each time.

0

You almost done it you can make extension to resize Image By K-Byte

extension UIImage {
    func resizeImageByKByte(kb: Int) {
        let maxByte : Int64 = Int64(kb * 1024)
        var maxCompressQuality: CGFloat = 1
        var imageByte : Int64 = Int64(UIImageJPEGRepresentation(self, 1)?.count ?? 0)
        while imageByte > maxByte {
            imageByte = Int64(UIImageJPEGRepresentation(self, maxCompressQuality)?.count ?? 0)
            maxCompressQuality -= 0.1
        }
    }
}

Used Like this :

 UIImage().resizeImageByKByte(kb: 200) // 200 KB
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35