UIImage File Extension

The UIImage class is a bit tricky to use when instantiating images from the Assets Catalog in Xcode. The tricky part is specified in the imageNamed class method of the UIImage class (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#jumpTo_19):

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.

So, you may see sample code that does not specify the extension when creating new images. This shortcut is only valid for PNG files so all other extension types need to specify. What makes it tricky is that the class method will return nil if the image is not found and the UIImageView.image property will accept this nil without complaining making it seem like everything is working.

Leave a Reply