How can I solve the memory problem using the imageWithContentsoffile function in iphone

I am facing a memory issue when adding a UIButton to a UITableView . Below is my code for setting a UIButton image:

 UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:image forState:UIControlStateNormal]; 

But when I use the imageName: method instead of the imageWithContentsOfFile: method, it works fine. Anyone have a good solution for this problem?

+4
source share
1 answer

imageWithContentsOfFile: vs imageNamed:

As far as I know,

imageNamed loads the image into a special system cache, and then future calls with this path will return the image to the cache, and not reload it from disk.

imageWithContentsOfFile just loads the image at the path you specify, but does not cache. Multiple calls to imageWithContentsOfFile for the same image will result in multiple copies in memory.

And about memory leaks, I'm not sure which one is better to use when using a large number of images in programming ...

0
source

Source: https://habr.com/ru/post/1443655/


All Articles