how to store image to sqlite using core data in iphone? [duplicate]
Possible Duplicate:
how to store thumbnail image to sqlite table using core data in iphone?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// Check if it's a photo or video and create MediaItem accordingly
if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:(NSString *)kUTTypeImage]){
NSLog(@"Image");
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.autocapita开发者_如何转开发lizationType = 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?
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.
精彩评论