TechChain
TechChain

Reputation: 8952

Memory increasing when loading image on image view?

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. ?

Upvotes: 2

Views: 844

Answers (1)

Bilal
Bilal

Reputation: 19156

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.

Upvotes: 1

Related Questions