2012-05-09 9 views
0

私はiOSの新鮮な人です。 ここにUIImagePickerControllerサンプルコードがあります私はUIImagePickerControllerでCoreDataについて知りたいです。

NSManagedObjectContext、NSEntityDescriptionを使用してデータを管理する理由を知りたいと思います。 値を直接設定しないのはなぜですか? 助けてくれてありがとう!

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo { 

NSManagedObjectContext *context = event.managedObjectContext; 

// If the event already has a photo, delete it. 
if (event.photo) { 
    [context deleteObject:event.photo]; 
} 

// Create a new photo object and set the image. 
Photo *photo = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:context]; 
photo.image = selectedImage; 

// Associate the photo object with the event. 
event.photo = photo;  

// Create a thumbnail version of the image for the event object. 
CGSize size = selectedImage.size; 
CGFloat ratio = 0; 
if (size.width > size.height) { 
    ratio = 44.0/size.width; 
} 
else { 
    ratio = 44.0/size.height; 
} 
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height); 

UIGraphicsBeginImageContext(rect.size); 
[selectedImage drawInRect:rect]; 
event.thumbnail = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Commit the change. 
NSError *error = nil; 
if (![event.managedObjectContext save:&error]) { 
    // Handle the error. 
} 

// Update the user interface appropriately. 
[self updatePhotoInfo]; 

[self dismissModalViewControllerAnimated:YES]; 

}

答えて

0

私はあなたが「直接画像を挿入します」と言うとき、あなたは何を意味するかの完全にわかりません。あなたのコードでは、新しいPhotoオブジェクトを作成して新しいPhotoエンティティをデータベースに挿入し、UIImagePickerViewControllerで選択した画像にその写真の属性を設定します.UlmagePickerViewControllerによって、データベースの特定のエンティティに画像が保存されます。

つまり、直接設定します。なぜそれがクエリを使ってデータベースに追加されていないのか疑問に思っているのであれば、それはCore Dataがオブジェクト指向のデータベース層であり、オブジェクト指向の明白な特色に加えて、NSManagedObjectContextは、すなわち、変更を後悔する能力。

関連する問題