I am facing memory issue when adding a UIButton to a UITableView. Below is my code for setting the UIButton‘s image:
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
But when I use imageName: method instead of imageWithContentsOfFile: method it’s working perfectly. Does anybody have good solution for this issue?
imageWithContentsOfFile: vs imageNamed:
As per my knowledge,
imageNamed loads the image in a special system cache, and then future calls with that image path will return the image in the cache instead of reloading it from disk.
imageWithContentsOfFile simply loads the image at the path you specify, but does no caching. Multiple calls to imageWithContentsOfFile for the same image will result in multiple copies in memory.
AND about memory leaks i am not sure which one is better from both to use while using large number of images in programming…