2

I am trying to compress an image using following method

jpegData(compressionQuality:)

In this case image got compressed but the resolution of an image also got changed. But i actually want only image will compress without changing the resolution. Please suggest the possible solutions.

  • 1
    If you need compression, you will have to forget about the resolution. Without lowering the resolution you can't compress. – nayem Dec 10 '18 at 09:09
  • Did you figure out the reason and a solution? Facing the same issue that `jpegData(compressionQuality:)` seems to apply screen scale to the resolution – hyouuu Feb 06 '20 at 05:16

1 Answers1

-1
extension UIImage {

    public func base64(format: ImageFormat) -> String? {
        var imageData: Data?
        switch format {
        case .png: imageData = UIImagePNGRepresentation(self)
        case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
        }
        return imageData?.base64EncodedString()
    }
}

use this extension to compress the image, i have been using this extension from a long time, try it if it does not affect the resolution.