Loading UIImage

January 26, 2011 2 comments

In iphone normally images are stored into Photo Album, Application Bundle, Sandbox, or Internet. All these sources allow application to store images data and to use that data into our iPhone application. In this article we will learn how to load UIImage objects from Application bundle, Sandbox and from internet using our application code.

iPhone supports following image types to load and save.PNG, JPG, TIF, TIFF, GIF, THM, ICO, JPEG, BMP, BMPF, CUR, XBM, PDF

Load Image from Application Bundle:

Normally we always use the UIImage’s class method imageNamed: to load image from application bundle like this

UIImage *myImage = [UIImage imageNamed:@"myImage.png"];

This method looks for an image int0 the top folder of application bundle, if the image is found its loaded and cached by iphone system then all its memory is managed by the iphone cache system. But this is not so simple, iPhone system’s image cache doesn’t respond properly to memory management warnings and does not release its objects. This may be not a problem for small applications and may not be a problem for applications which loads frequently small images. But the application which carefully use the memory allocation and its release and heavily use large images should use the substitute method imageWithContentOfFile: for imageNamed: This method loads an image from provided path as its argument, to get a path for an image we use the NSBundle class method to find the path for a given resource.
This example loads an image “myImage.png” from the top level folder of application bundle.

NSString *pathOfImageFile = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
UIImage *myImage = [UIImageimageWithContentOfFile:pathOfImageFile];

Load Image from Sandbox:

Applications Sandbox consist of three folders: Documents, Library and tmp.
Documents folder does exactly the name suggests, its used to store documents and to retrieve documents while Library folder stores some user defaults and state information and tmp folder is used to create files temporarily. We need to understand the difference between these folders before storing any kind of data into them. When iTunes backups our application it only backups Documents and Library folders and discard the tmp folder, so we always need to store our temp data into it.

this is the way to locate the document folder into iPhone applications, The results of NSHomeDirectory() lets you navigate down one level to documents with full assurance of reaching the proper destination.

NSString *documentsFolder()
 return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

To load your image, append its file name to the returned path and tell UIImage to create a new image with those contents. This code loads a myImage.png from the top level of Documents folder and returned a UIImage instance initialized with that data.

NSString *pathOfImageFile = [documentsFolder() stringByAppendingPathComponent:@"myImage.png"];
return [UIImage imageWithContentsOfFile:pathOfImageFile];

Load Image from Internet:

+ (UIImage *) imageFromURL: (NSString *)urlString
 NSURL *url = [NSURL URLWithString:urlString];
 return [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

dataWithContentsOfURL: is used to download an image from a URL in its argument,
then imageWithData: method of UIImage class is used to construct an image from NSData argument.

NSURL *url = [NSURL URLWithString: @"http://abc.com/images/myImage.jpg"];
UIImage *myImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];

Utility function:

GetFolderPath() is used to returned the path of our desired folder, it appends the images folder to the documents folder path.

GetImages() method is used to read all images of type “JPG” from the GetFolderPath, and GetImageNamed() is used to returned the named image from GetFolderPath().

NSString *GetFolderPath()
 return [NSHomeDirectory() stringByAppendingPathComponent:@"../MyImages"];

+ (NSArray *) GetImages
 NSString *file;
 NSMutableArray *results = [NSMutableArray array];
 NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:GetFolderPath()];
 while (file = [dirEnum nextObject]) if ([file hasSuffix:@"JPG"]) [results addObject:file];
 return results;
+ (UIImage *) GetImageNamed: (NSString *) aName
 NSString *path = [ImageHelper pathForItemNamed:aName inFolder:GetFolderPath()];
 return [UIImage imageWithContentsOfFile:path];
Categories: iPad, iPhone, iPod, ObjectiveC Tags: , ,