Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UIImage not displaying JPEG images

I'm having trouble displaying some of the images in my iOS app. I have changed some of the graphics from PNG to JPG file format and when I removed the PNG images already in the project and added the new JPGs, the simulator was still showing old PNG graphics. I ran 'clean' on the project and reset simulator's contents, and now it just does not want to load the images at all - [UIImage imageNamed:@"filename"] returns null.

On the other hand, if I save those graphics as PNG, and add them to the project instead of JPEGs, it loads them fine. But the images are photographic and PNG versions are 7-8 times larger than JPEG, so I'd really want to figure out why it won't load them.

Edit: after reviewing the documentation (for what seems like 100th time), I realized that the mistake was that I did not specify the extension. The Supported Image Formats (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html) lists quite a few formats that are supported, and UIImage reference says that:

On iOS 4 and later, if the file is in PNG format, it is not necessary to specify the .PNG filename extension.

I misread that to mean that the extension is optional in general, but if the file is not a PNG, you have to include it. So if the file is called my_image.png, then you can pass it as @"my_image" or @"my_image.png", but if you have my_image.jpg, it will only work as @"my_image.jpg".

like image 669
SaltyNuts Avatar asked Jan 14 '23 02:01

SaltyNuts


2 Answers

the method imageNamed: only load *.png resorce in project. Try

  NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"imageName"] ofType:@"jpg"];
  UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
like image 177
Nathan Ou Avatar answered Jan 15 '23 17:01

Nathan Ou


http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html

You see 'Supported file format' of UIImage to know some format is supported in IOS.

like image 25
Nguyen Thi Thuy Trang Avatar answered Jan 15 '23 16:01

Nguyen Thi Thuy Trang