Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: will using [UIImage -imageNamed:] without file extension cause performance issue?

I have a image named [email protected].The file is 1KB in size. My app is a iPhone-only app.

I load the image in my app with this call:

[UIImage imageNamed:@"image"];

When using I/O activity instrument analyzing the I/O activities, I found that there are several activities relating to this file which result in "No such file or directory" error, and these activities do take some time.

these activities are like reading these files:

image_2_only_@2x~iphone.png
image@2x~iphone.png
[email protected] (this one doesn't fail)

And to my surprise, every failed operations takes more time than the one succeeded.

How can I avoid this kind of performance issue?

like image 232
CarmeloS Avatar asked Mar 06 '13 03:03

CarmeloS


1 Answers

If you want to prevent extensive file-system access, do not use [UIImage imageNamed:] but [UIImage imageWithContentsOfFile:].

The latter will not do any automised selection of the image (extension guessing, retina guessing, ...) and it will not use memory caching.

Make sure that you are keeping in mind that any duplicate access on an image on the filesystem will, when using my suggestion, be punished very hard.

I am not recommending to not use imageNamed: for improving the performance of an App. Usually the opposite is true and image loading will be more optimised (caching) when using that method. However, once you are certain that a specific image will not be loaded again and for preventing excessive filesystem lookups, use imageWithContentsOfFile:. Additionally, it is actually recommendable to use imageWithContentsOfFile: when memory usage is a point - no caching means less hogged memory.

like image 120
Till Avatar answered Nov 09 '22 00:11

Till