0

次のソース ファイルから、画像を読み込んで Core Data に保存し、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 に答える