保存图像到核心数据

问题描述:

从以下源文件,我尝试加载图像并保存到核心数据,也显示在imageView中,但它不工作 你能帮我解决它。保存图像到核心数据

- (IBAction)save:(id)sender 
{ 
    NSLog(@"Telling the AddTrackingTVC Delegate that save was tapped on the  AddTrackingTVC"); 

    [self.delegate theSaveButtonOnTheAddPlantNoteTVCWasTapped:self]; 

    MyPlant *myTracking = [NSEntityDescription insertNewObjectForEntityForName:@"MyPlant" inManagedObjectContext:self.managedObjectContext]; 

    myTracking.name = nameTextField.text; 
    myTracking.detail = detailTextField.text; 
    myTracking.createdDate = [NSDate date]; 
    //myTracking.photo = [NSData ]; 
    [self.managedObjectContext save:nil]; // write data to database 
    [self.delegate theSaveButtonOnTheAddPlantNoteTVCWasTapped:self]; 
} 




- (IBAction)takePicture:(id)sender { 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    // If our device has a camera, we want to take a picture, otherwise, we // just pick from photo library 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera]; } 
    else { 
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; } 
    // This line of code will generate a warning right now, ignore it 
    [imagePicker setDelegate:self]; 
    // Place image picker on the screen 
    [self presentViewController:imagePicker animated:YES completion:nil]; 
} 


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    self.currentEntry.photo = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 1.0); 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

所以我有这些功能做,使之能够保存图像核心数据?

Download source files

+1

请注意,大多数人都持谨慎态度(最好)约下载.zip文件。如果您发布一些相关的代码,并且突出显示可能给您带来麻烦的部分,我们就会更容易理解该问题。 – 2013-03-09 19:32:32

+0

请确定问题中的相关源代码部分,并将其包含在您的问题中。 – 2013-03-09 19:47:03

+1

我真的建议将图像保存到磁盘并在核心数据中保存*路径* – Hyperbole 2013-03-09 21:08:21

这是我要做的事:

NSURL * imageURL = [NSURL URLWithString:@"http://www.website.com/picture.png"]; 
NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
UIImage * image = [UIImage imageWithData:imageData]; 
NSData *savedImageData = UIImagePNGRepresentation(image); 
[coreDataObject setValue:savedImageData forKey:@"image"]; 

而当你想要显示它:

imageView.image = [UIImage imageWithData:[coreDataObject valueForKey:@"image"]];