8

I have a image named image@2x.png.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
image@2x.png (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?

CarmeloS
  • 7,868
  • 8
  • 56
  • 103
  • somewhat related - http://stackoverflow.com/questions/7599030/uiimage-imagenamed-extension – Krishnabhadra Mar 06 '13 at 03:42
  • @CarmeloS . got the answer for "without file extension cause performance issue?" ? – name-it Nov 28 '13 at 15:22
  • @Rajapandian see the selected answer : ) – CarmeloS Nov 29 '13 at 14:04
  • @CarmeloS thanks for the response. it discusses about the other options "imageWithContentsOfFile". i didn't see anything that discusses the extension/without extension on "imageNamed" if i'm not wrong. – name-it Nov 30 '13 at 11:32

1 Answers1

5

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.

Till
  • 27,559
  • 13
  • 88
  • 122
  • if the latter doesn't do any caching or any optimizations, then should Dwayne be using "`imageNamed`" like he's already doing? – Michael Dautermann Mar 06 '13 at 03:50
  • Oh, I don't think OP want to avoid extension guessing, what he want is to avoid the performance issue because of it. Also image caching is very important feature that positively affect the performance. I don't think this answers the question. – Krishnabhadra Mar 06 '13 at 03:51
  • Caching does only improve the performance for cache-hits. So I assumed the OP would be able to prevent misses. – Till Mar 06 '13 at 03:52
  • Does using imageWithContentsOfFile: instead of imageName: request caller to check resolution of screen add add @2x suffix? – CarmeloS Mar 07 '13 at 02:16
  • @Dwayne yes, you have to add any possible suffixes yourself - the system wont try to guess anything. – Till Mar 07 '13 at 02:20
  • OK, I got it. Besides, do you know the name searching order of imageNamed:, I thought maybe I could rename my files to combine the advantages of both imageNamed and imageWithContentsOfFile: by naming my images as the first name that imageNamed: searches for. Is that possible? – CarmeloS Mar 07 '13 at 02:46
  • @Dwayne no, I do not know anything about the ordering. Even if I knew, I would not rely upon it as that is subject to change at Apple's will as long as they do not document it. – Till Mar 07 '13 at 02:56