Getting really slow performance using imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate

I have a simple table view with images created using the following:


imageView.image = [[UIImage imageNamed:iconName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];


This was working fine until I refactored the code to use a custom table cell and autolayout (was using generic UITableViewCells with tags).


Now it takes a long time to render and shows out of memory errors when simply scrolling half a dozen rows! Any idea what's wrong or how to fix?

Try using instruments to time profile imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate. From when I had profiled it, the method was very expensive to call. It was not noticable when calling it a few times to layout static elements on a screen but calling it when dequing each tableViewCell is not recommended and will noticable impact the smoothness of the tableview scroll.


I assume you are using this method in order to apply your own color to the image. Based on how many images you have, try to come up with another solution, perhaps precoloring all the images before loading the tableview cells, etc.


Based on your example line of code

[[UIImage imageNamed:iconName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

You have only one image that you are loading. You can load this image in viewDidLoad and cache its template version so that you are not making this call many times in cellForRowAtIndexPath:


self.cachedImage = [[UIImage imageNamed:iconName] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];


Then in CellForRow

imageView.image = self.cachedImage;

Getting really slow performance using imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
 
 
Q