Image Compression without changing the resolution of an image in swift

318 Views Asked by At

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

There are 1 best solutions below

2
On
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.