Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image paths from NSBundle in Objective C?

I have about 60 images I want to store in Core Data, 30 of which are avatars and have the prefix of avt_filename_00X.png and 30 of them are smaller and have a different prefix.

Rather than storing all the images as BLOBs in Core Data/SQLite, I want to store the paths for each image found (in the same way you would store image paths for a MySQL database).

However I am not sure how to grab the path of the image as found in NSBundle.

I can get the path to the NSDocumentDirectory via:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:documentsDirectory];

NSLog(@"documentsDirectory = %@", documentsDirectory);

And I can load the image and add it to an array.

if (qty == 0)
{
    //NSLog(@"fileToLoad = %@", fileToLoad);

    UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileToLoad ofType:fileExt]];

    [self.avtList addObject:img];

    [img release];

} else {

    // load multiple image into an array
    // not coded yet
}

But, what I'm unsure of is:

  1. How do I grab the path where the computer found the image once its inside the NSBundle?

  2. How can I be sure that the path will work when the app is on a device?

The idea would be to get all the images stored in an array and then push them to Core Data/SQLite at a later time.

like image 411
zardon Avatar asked Oct 28 '10 15:10

zardon


2 Answers

The correct way to get the full path to a resource in the main bundle is:

[[NSBundle mainBundle] pathForResource:@"avt_filename_00X" ofType:@"png"]

(or you can supply the empty string for 'ofType' if you prefer to include the extension in the resource name)

But nowhere in the docs is the path guaranteed to remain the same across devices, operating system iterations, etc. It's the path to that file from the application bundle in the current environment, guaranteed to remain valid for the duration of this run of the application only.

Because the path to the application, and hence to its resources, isn't guaranteed to stay the same, I think it's explicitly unsafe to put it in an SQL database by any means.

Could you perhaps adopt a scheme whereby a filename starting in / is a complete path, one without a / at the start is assumed to be in the bundle, meaning that you can apply the logic on the outside of the database?

like image 151
Tommy Avatar answered Nov 02 '22 07:11

Tommy


How can I be sure that the path will work when the app is on a device?

Therein lies the rub: you can't. You would be best to let the paths be handled on-the-fly, and perhaps just store the file names instead.

like image 2
Philip Regan Avatar answered Nov 02 '22 07:11

Philip Regan