UIImage Optimization

When do we need to use UIImage(named:) and UIImage(contentsOfFile:) ?

UIImage Optimization

When do we need to use UIImage(named:) and UIImage(contentsOfFile:) ?

We all have used both the api’s but only few developer’s know the importance of these api and can decide when to use which one. lets discuss one scenario so that we can understand and remember about these api’s..

Problem:
In my iOS app written with swift, I set the image from local file on UIImageView with following code: someImageView.image = UIImage(named: documentsDirectory[0].stringByAppendingPathComponent("myImage.jpeg")) and that works just fine.
At some stage (in my case another ViewController), I have to rewrite the file content by either deleting the existing file first and then writing new file to that directory or by moving new file to existing directory. After the file is rewritten the image view keep showing the old image even if I reset its image with the same method as mention above.If I close the app then reopen it the new image is shown.
I know that this is probably a cache issue , lets find out which api should be use for what condition..

Solution:
You’re running into a couple of different problems, the biggest of which is that UIImage.imageNamed specifically caches the image read, so changing the underlying file won’t result in the new image being used. The other (related) issue is that imageNamed is primarily intended to load static images out of the application bundle.

Switch to using UIImage(contentsOfFile:...) and make sure you're reloading the image when the backing file is changed.

UIImage(named:...) vs UIImage(contentsOfFile:...)

imageNamed: imageNamed cache’s your images and you lose control over the memory — there’s no guarantee that releasing the object will actually release the image but does provide faster loading of images second time around as they are cached. If you are using Interface Builder, and setting the image in Image View Attributes, that is also equal to imageNamed method. The image will be cached immediately when the app is ran

imageWithContentsOfFile : imageWithContentsOfFile does not cache images and is more memory friendly however as it does not cache images and they are loaded much slower. imageWithContentsOfFile: requires you to put the full path. I don’t see why imageNamed: wouldn’t be recommended, besides the fact that you can’t access files outside of the application bundle.

Discussion

imageNamed method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method locates and loads the image data from disk or from an available asset catalog, and then returns the resulting object.

The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used.

In iOS 9 and later, this method is thread safe.

Special Considerations

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

Hope you like this article & is useful for people looking to find out when to use these UIImage API’s, Please ❤️ to recommend this post to others 😊. Let me know your feedback. :)

References:

  1. https://stackoverflow.com/questions/27907107/ios-rewriting-image-file-at-specific-path-shows-old-image