0

从以下源文件中,我尝试加载图像并保存到核心数据并在 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];

}

那么我必须用这些功能做什么才能使其能够将图像保存到核心数据?

下载源文件

4

1 回答 1

5

我就是这样做的:

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"]];
于 2013-03-09T19:29:48.170 回答