2

I am getting my image from PHAsset.

I am requesting an image data using requestImageData function of PHAsset.

After getting data from that function, I create an image from that data using scale = 1

Then again converting data from that newly created image, I get increased data, not as the data I got previously

Below is the code that I use to get data from PHAsset

imageManager.requestImageData(for: image.asset, options: nil) { [weak self] (data, str, orientation, info) in
            guard let strongSelf = self else { return }
            if let dt = data {
                //Here I get data.count as 4993397
                if let image = UIImage(data: dt, scale: 1) {
                    strongSelf.image = image 
                    print("New data : \(image.jpegData(compressionQuality: 1)?.count)")
                    //The result printed here is 12107879
                }
            }
        }

Why the data gets increased here, I don't have any idea

Thanks!!

Krima Shah
  • 117
  • 1
  • 8
  • There is [really good comment](https://stackoverflow.com/a/50582203/5928311) about size of image and about compression ratio. This is not the answer to your question, but just FYI – Vadim Nikolaev Dec 27 '19 at 13:11
  • @VadimNikolaev Thanks for the response, I'll surely look for this – Krima Shah Dec 27 '19 at 15:22

1 Answers1

2

This line

let image = UIImage(data: dt, scale: 1)

construct image with the highest quality ratio (q=1.0). That’s why the image is suddenly so big.

So the moment you get your image as an UIImage from NSData, the decompression/conversion changes the size of the image.

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
  • Thanks for the response, I understood what you are saying but how can I solve this problem because I want to get data from newly created image, should I resize image at that moment when I get the UIImage from Data ? @jawadAli – Krima Shah Dec 28 '19 at 04:10
  • There are proper ways to resize an image such as https://stackoverflow.com/questions/31314412/how-to-resize-image-in-swift – Jawad Ali Jan 02 '20 at 07:15
  • I have resized my image with this kind of resizing technique. Thanks for the response – Krima Shah Jan 02 '20 at 09:44
  • thank you :) have a nice day ... if it resolved please accept answer – Jawad Ali Jan 02 '20 at 09:49
  • sorry for the delay in accepting the answer @jawadAli – Krima Shah Jan 02 '20 at 12:03