Take the 2-minute tour ×

Possible Duplicate:
how to store thumbnail image to sqlite table using core data in iphone?

    // Check if it's a photo or video and create MediaItem accordingly
    if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:(NSString *)kUTTypeImage]){
        PhotoItem *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoItem" inManagedObjectContext:context];
        [photo setImage:[info valueForKey:@"UIImagePickerControllerOriginalImage"]];
        currentItem = [photo retain];
//        listcell=[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
//        [listcell setItemText:@"Photo"];

        nameAlert = [[UIAlertView alloc] initWithTitle:@"Enter Photo Name" message:@" " delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
        /*CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 150);
        [nameAlert setTransform:transform];*/
        nameField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 245, 20)];
        nameField.backgroundColor = [UIColor whiteColor];
        nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        nameField.delegate = self;
        [nameAlert addSubview:nameField];
        [nameAlert show];
        [nameAlert release];

I am using above code to store image from photo gallery.i need to store thumbnail to database.how to do that?

share|improve this question
i am not having error here. I have no idea how to store image so as i asked with code. –  Heena Dave Jun 30 '11 at 9:54
If you want to use Core Data, first you have to learn Core Data. Then write a model, initialize Core Data with a hundred lines of boilerplate code, write a thumbnail class, save the image as binary into it and save the changes. Instructions would be too long to post here. An alternative is to write the file to disk and save the path to a plist. I would use a md5 of the file as filename. –  Jano Jun 30 '11 at 10:04
i am learning core data step by step as i need.i will try your alternative of writing file to disk and save that path to plist. –  Heena Dave Jun 30 '11 at 10:18
This is the third time you have asked this question: 1. 2. Please don't do that. –  Josh Caswell Jun 30 '11 at 18:16
add comment

marked as duplicate by Robert Harvey Jun 30 '11 at 18:24


1 Answer

There are two methods for storing images with Core Data:

(1) Set a Core Data entity attribute to type "transformable". A transformable attribute accepts any object can transforms it to raw data that is saved to the persistent store. When you read the attribute it reverses the process. Since UIImage does not implement NSCoding protocol you have to provide a custom value transformer. See the Core Data Programming Guide:Non-Standard Persistent Attributes for an introduction.

(2) However, the system overhead for (1) is fairly high so you usually don't store images in the persistent store directly but instead store them as external image files and then store just the file path in Core Data. This is faster and less memory intensive.

Most people use option (2). It is also easier for Core Data novices to implement.

share|improve this answer
add comment
