2

My app downloads a small image file from a remote server and I am trying to display it along with some other small image files that are pre-installed in the app. I am using [UIImage imageNamed:@"TheImageName.png"] to get the images (see below for more detail). The pre-installed images display as expected but the image in my apps 'Documents' directory is no where to be found. Should I be using -imageNamed for an image in 'Documents' or some other method?

UIImage* documentsImage = [UIImage imageNamed:@"TheImageName.png"];
UIImageView* anImageView = [[UIimageView alloc] initWithImage:documentsImage];
[self.view addSubview:anImageView];
RexOnRoids
  • 14,002
  • 33
  • 96
  • 136

2 Answers2

2

Ah, nevermind. I found that using [UIImage imageWithContentsOfFile:filePath] works for this. imageNamed just returns nil.

RexOnRoids
  • 14,002
  • 33
  • 96
  • 136
  • 1
    Yup, notice that imageNamed: doesn't only loads the image, it also caches it in memory. While imageWithContentsOfFile: doesn't. – pablasso Jul 03 '12 at 21:23
1

No, +imageNamed: only loads images from the app bundle.

Also, don't include the .png extension when using that class method.

smokris
  • 11,740
  • 2
  • 39
  • 59
  • 1
    More specifically, "On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension. Prior to iOS 4, you must specify the filename extension." -- http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageNamed: – Matt Mc Jun 29 '13 at 02:43