0
NSURL* urlEx = [NSURL URLWithString:@"Pacific_Map.png"];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];

UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];

mapImageViewEx.image = imgEx;

If I replace mapImageViewEx.image = imgEx; with mapImageViewEx.image = [UIImage imageNamed:@"Pacific_Map.png"]; it works, but I do not want to use imageNamed.

Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90

2 Answers2

2

That's because imageNamed knows where to scan for the file, but initWithContentsOfURL expects the full path. use

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"Pacific_Map" ofType:@"png"];
NSURL* urlEx = [NSURL fileURLWithPath:imagePath];
NSData* mapExIMGData = [[NSData alloc] initWithContentsOfURL: urlEx];

UIImage* imgEx = [[UIImage alloc] initWithData:mapExIMGData];

mapImageViewEx.image = imgEx;
Zaky German
  • 14,324
  • 4
  • 25
  • 31
1

What's wrong with using imageNamed? Anyway, you could use initWithContentsofPath.

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

Tell us more about what you are trying to accomplish. Good luck,

James

EDIT: Sorry, I assumed Pacific_Map.png was just a placeholder path. Like someone else posted, you need to indicate the full path if you're not going to use imageNamed.

James
  • 2,272
  • 1
  • 21
  • 31
  • I don't want to cache the image. – Chewie The Chorkie Mar 11 '11 at 20:59
  • Check this posting out. http://stackoverflow.com/questions/316236/uiimage-imagenamed-vs-uiimage-imagewithdata Anyway, imageWithContentsOfFile does not cache images, so that could be viable for you. Still, depending on what you're doing, caching the image could actually reduce your memory usage... Explained better in the posting. – James Mar 11 '11 at 21:03