Vikings
Vikings

Reputation: 2527

iPhone Saving Images

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

Answers (3)

Yogev Shelly
Yogev Shelly

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:

  • Avoid duplicate names so you don't override exisitng files (don't worry you'll get a warning)
  • Remember to delete the photo when the model is being deleted
  • Build a logical path and folder structure that matches your model, e.g - album.photo should have a folder named album and in it you should store the files, helps when you delete the album model

Upvotes: 4

Pierre
Pierre

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

Aravindhan
Aravindhan

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

Related Questions