Get image paths from NSBundle in Objective-C?

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

Instead of storing all the images as a BLOB in Core Data / SQLite, I want to save the paths for each image found (just like you saved the image paths for the MySQL database).

However, I am not sure how to capture the image path found in the NSBundle.

I can get the path to NSDocumentDirectory through:

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 the 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 I am not sure of the following:

  • How can I capture the path where the computer found the image after it inside the NSBundle?

  • How can I be sure that the path will work when the application is on the device?

The idea would be to get all the images stored in the array and then output them to Core Data / SQLite later.

+7
objective-c image path core-data nsbundle
source share
2 answers

The correct way to get the full path to the resource in the main package:

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

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

But nowhere in the documents is it guaranteed that the path will remain unchanged on all devices, iterations of the operating system, etc. This is the path to this file from the application package in the current environment, which is guaranteed to remain valid throughout this entire application run.

Since the path to the application and, therefore, to its resources is not guaranteed to remain unchanged, I think that it is clearly unsafe to put it in the SQL database by any means.

Perhaps you can accept the scheme according to which a file name starting in / is the full path, it is believed that one of them does not have / at the beginning, means that you can apply logic on the outside of the database?

+25
source share

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

There is rub in this: you cannot. It would be best to enable path management on the fly and maybe just save the file names.

+2
source share

All Articles