2012-05-09 2 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를 통해 선택한 이미지로 사진 속성을 설정합니다.이 이미지는 데이터베이스의 특정 엔터티에 이미지를 저장합니다 .

즉, 직접 설정해야합니다. 쿼리를 사용하여 데이터베이스에 추가되지 않는 이유가 궁금하다면 Core Data는 객체 지향 데이터베이스 레이어이고 객체 지향적 인 명백한 특권 외에도 NSManagedObjectContext는 많은 유용한 기능을 제공합니다. 즉. 변화를 후회하는 능력.

관련 문제