Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image programmatically in iOS

Tags:

ios

uiimage

In my App for the iPad I'm trying to load an image file programmatically. The file is NOT part of my project, hence it is not referenced in XCode. There is no entry for the file in XCode's Groups and Files column.

The image has to be loaded at runtime instead, it name being read from a Property List.

I'm trying to load the file like this:

NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"png" inDirectory:@"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];

In this case, mydirectory lives in the main bundle like this:

MyAmazingApp.app/MyDirectory/MyImage.png

Unfortunately, the Image will not load. I can't add the image file to my project, as its name is to be determined at runtime and cannot be known in advance. The file name is read from a config file (a Property List) at runtime instead.

What am I doing wrong? Your help will be very much appreciated.

like image 970
MatthiasC Avatar asked Feb 24 '11 14:02

MatthiasC


1 Answers

You can use NSFileManager to get the contents of a directory, or several directories. You obviously have to know something about the file you want to load so that you can identify it, but you could for example use NSFileManager to help you generate a list of images in your app bundle and in your app's Documents directory.

like image 116
Caleb Avatar answered Oct 15 '22 07:10

Caleb