2

I am loading image from url on a image-view. If the resolution of the image is 4000 * 2400 ,size of image is 1.6MB then when I load image. Memory increase upto 30MB just for image load. But if size is less than 10 KB then size does not increase. I know I must use small image for loading as thumb image but just for learning I want to know this.

I am using below code to load image

self.imgStore.kf.setImage(with: url, placeholder: #imageLiteral(resourceName: "placeholder"), options: nil, progressBlock: { (val, val1) in        }) { (image, error, cache, url) in
    DILog.print(items: "image recived")
}
  1. Why memory size increase upto 30 MB when image size is just 1.5 Mb ?
  2. How can I handle if image from server is large but memory in app should not increase. ?
TechChain
  • 8,404
  • 29
  • 103
  • 228

1 Answers1

1

1.5MB is compressed size. PNG's are decompressed in memory and it consume memory on pixel basis (RGBA). So the the size will be 4000 * 2400 * 4 ~= 38MB

If your image are too big and you can't change the resolution.

  1. Download the image and save directly on the disk.
  2. Resize the image before loading it in memory. See this for more info.
Bilal
  • 18,478
  • 8
  • 57
  • 72
  • Thanks for your explanation. Can you tell me how can i reduce the size of image so that size does not increase ? – TechChain Jan 12 '18 at 09:31
  • By reducing the resolution from `4000 * 2400` to what's required in your app. Or if you can't do that see my edited answer. – Bilal Jan 12 '18 at 09:36
  • Image in memory size is directly proportional to image resolution. – Bilal Jan 12 '18 at 09:39