I just ran into the problem described in the following question:
I have an application where you can attach an image to an item, and if you don't want you can use a default image.
All the items are 'saved' using Core Data, so basically there is a 'imageFilePath' attribute for the Item Entity.
If you choose a default picture, then the imageFile path is set to:
imageFilePath = [[NSBundle mainBundle] pathForResource:@"defaultImage1" ofType:@"png"]
The issue is that with iOS8, this path is not valid anymore on the next run, therefore the image is seen as missing by my application.
What would you recommend as the best solution for this issue?
1) Maybe i could set in the model that this is a bundle resource, instead of putting the full path? I will then have two kind of resources.
2) I could copy the needed resource in the Document Directory on first application startup?
Thx.
---Edit---- The Document Directory is also 'moving', so 2) is not really an option..
--- 2nd Edit ---
So i went for the following approach. I introduced a new Object 'ImageStore', that can store/retrieve images using a key, and as a bonus it can cache the created UIImages.
@interface MyImageStore : NSObject
+ (id) sharedStore;
- (NSString*) imagePathForKey:(NSString*) key;
- (void) setImageData: (NSData*) data forKey: (NSString*) key;
- (UIImage*) getImageForKey: (NSString*) key;
- (void) deleteImageForKey: (NSString*) key;
- (void) clearCachedImages;
@end
And now the Item entity has an imageFileKey attribute instead of imageFilePath.
I will take this approach as it is the one used in the Big Nerd Ranch book: