Reputation: 2527
I have a custom cell with a thumbnail image, which the user can select from their photo albums. I know that saving images to Core Data will result in poor performance, and I should use the file system.
Anyone recommend some good tutorials on this. What exactly should I store in core data, if I am not storing the image there.
I will be storing the larger image as well, not just the thumbnail.
Upvotes: 2
Views: 935
Reputation: 1373
UIImage*image = [UIImage imageNamed:@"the image you wish to save.jpg"];
//build the path for your image on the filesystem
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPath objectAtIndex:0];
NSString* photoName = @"imageName.jpg";
NSString *photoPath = [docsDir stringByAppendingPathComponent:photoName];
//get the imageData from your UIImage
float imageCompression = 0.5;
NSData *imageData = UIImageJPEGRepresentation(image, imageCompression);
//save the image
if(![imageData writeToFile:path atomically:YES]){
return FALSE;
}
//store the imagePath to your model
photoModal.filePathImage = photoPath;
TIPS:
Upvotes: 4
Reputation: 11673
You should have two fields in your core data db : thumbnailPath
: NSString
and originalPath
: NSString
With the file manager you create your thumbnail and original image at a specific path. so you'll have : ..../thumbs/myPicture_thumb.jpg AND ..../originals/myPicture.jpg Then you just store this two path to your core data db.
When you wan to display one of them you juste use UIImage
method : imageWithContentsOfFile
That's all
Upvotes: 2
Reputation: 15628
You can store the Images in the Application Documents Folder
and save the path of the Images in CoreData or sqlite. Refer the Images with their paths.
Upvotes: 1