0

I have an image and I need to save it without loss of quality or resizing. The file size should be as small as possible. But I can't even save it with its original file size.

I have an image.jpg (file size 2.1Mb). I put it in bundle and read in a Swift code. After I extracted it to PNG with .pngData() - and save as .png the file size is 18.1 Mb - which is ridiculous, even if I remove the Alpha Channel - it still will have 9-12 Mb.

If I use .jpegData() - it is still much more than the source image Only if I get jpeg with the quality 0.65% - I can have 2.1Mb - the same as the source but with visible loss in quality.

Where is the solution? How to optimize it and save without quality losses and without file size inflating?

 func foo() {
    
    let bundleURL = Bundle.main.bundleURL
    // Source file size: 2.1 MB
    let assetURL = bundleURL.appendingPathComponent("sourceBundle.bundle/image.jpg")
    let srcUImage = UIImage.init(contentsOfFile: assetURL.relativePath)

    if let dataPng = srcUImage!.pngData() {
        printSize(data: dataPng) // Size: 18.1 MB
    }
    if let dataJpg = srcUImage!.jpegData(compressionQuality: 1.0) {
        printSize(data: dataJpg) // Size: 5.3 MB
    }
    if let dataJpg = srcUImage!.jpegData(compressionQuality: 0.65) {
        printSize(data: dataJpg) // Size: 2.1 MB
    }
}

func printSize(data: Data) {
    let bcf = ByteCountFormatter()
    bcf.allowedUnits = [.useMB]
    bcf.countStyle = .file
    let string = bcf.string(fromByteCount: Int64(data.count))
    print("Size: \(string)")
}
Yura Buyaroff
  • 1,718
  • 3
  • 18
  • 31
  • What is the format of your original file? What compression was used when creating it? If you have your file already compressed why do you need to recompress it instead of just copying it? – Leo Dabus Mar 21 '21 at 23:04
  • @LeoDabus the original is image.jpg A user of mobile app has an image on the device. Nobody knows how the image was compressed. We don't care. The app has an option of uploading the file on server. And we have a possibility to crop the image. Of course I don't need the image from 2Mb become 20 Mb. – Yura Buyaroff Mar 21 '21 at 23:15
  • PNG had no options to compress the image. If the user is uploading the image from his device there is a big chance that the image format is HEIC not jpeg. – Leo Dabus Mar 21 '21 at 23:26
  • What you need is to scale and compress the image. If you want to keep the image resolution just upload its data. – Leo Dabus Mar 21 '21 at 23:27
  • 1
    possible duplicate of [How to compress of reduce the size of an image before uploading to Parse as PFFile?](https://stackoverflow.com/questions/29726643/how-to-compress-of-reduce-the-size-of-an-image-before-uploading-to-parse-as-pffi/29726675), [How do I resize the UIImage to reduce upload image size](https://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size/29138120#29138120) and [convert from UIImage to HEIC Data](https://stackoverflow.com/questions/61830224/how-can-i-convert-from-uiimage-to-heif-heic-data-in-swift/61830766#61830766) – Leo Dabus Mar 21 '21 at 23:33
  • @LeoDabus I saw all these answers, thank you. Please read my question. It is not what I need. I have the JPG image 2.1Mb, Somebody could compress it in this way. My question was how to save image without resizing, without quality loss. How to compress it. Photoshop can compress source img in PNG as 13Mb which is less than 18Mb, but still a lot. But I want to compress it to JPG 2.1MB. Somebody could do it. That means it is possible. – Yura Buyaroff Mar 21 '21 at 23:41
  • Just don't compress it again. If the image is already compressed just upload its data. Btw iOS is not Photoshop. – Leo Dabus Mar 21 '21 at 23:42
  • Your comments don't seem to make sense? if you have an image of 2.1Mb you don't need to compress it to 2.1Mb - you already have what you need? – flanker Mar 22 '21 at 01:36
  • The user has an image 2.1Mb on the mobile device. The user upload this image via mobile app to the server. It is possible the image will be modified, cropped, whatever... then it will be uploaded to the server as JPG. So I have to decompress it and compress again. I didn't ask "Do I need decompress and then compress?". I'm asking "How to compress the JPG image in much efficient way compared to the default iOS solution? Because it is possible in general, but iOS default method sucks". Does this comment has sense now? – Yura Buyaroff Mar 22 '21 at 01:55

0 Answers0