2011-09-27 105 views
1

我使用ABPeoplePickerNavigationController將iPhone聯繫人導入到我的應用程序。我最近決定,不是隻保存縮略圖圖像,而是保存整個圖像。有了iPad並且可能需要更大尺寸的圖像以及不同的裁剪,我認爲這是一條路。但是,在導入後保存一些新的聯繫人時,根據照片的大小,保存時間會很長。沒有照片,保存是即時的,所以我知道這與照片的大小以及我正在做的事情有關。相關的代碼是在以下情況下,任何人都可以指出我在做什麼錯?ABPeoplePickerNavigationController節省更大的圖像需要很長的時間

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)contact { 

    NSLog(@"%s", __FUNCTION__); 
    self.selectedThumbnailImage = nil; 
    self.selectedImage = nil; 

    if(ABPersonHasImageData(contact)){ 

     self.selectedThumbnailImage = nil; 
     NSData *imgData = (NSData *)ABPersonCopyImageData(contact); 
     UIImage *pickedImage = [UIImage imageWithData:imgData]; 
     [imgData release]; 

     self.selectedImage = pickedImage; 

     CGSize imageSize = pickedImage.size; 
     CGSize targetSize = CGSizeMake(110.0,120.0); 
     CGFloat width = imageSize.width; 
     CGFloat height = imageSize.height; 
     CGFloat targetWidth = targetSize.width; 
     CGFloat targetHeight = targetSize.height; 
     CGFloat scaleFactor = 0.0; 
     CGFloat scaledWidth = targetWidth; 
     CGFloat scaledHeight = targetHeight; 
     CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

     if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
     { 
      CGFloat widthFactor = targetWidth/width; 
      CGFloat heightFactor = targetHeight/height; 

      if (widthFactor > heightFactor) 
       scaleFactor = widthFactor; // scale to fit height 
      else 
       scaleFactor = heightFactor; // scale to fit width 
      scaledWidth = width * scaleFactor; 
      scaledHeight = height * scaleFactor; 

      // center the image 
      if (widthFactor > heightFactor) 
      { 
       thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
      } 
      else 
       if (widthFactor < heightFactor) 
       { 
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
       } 
     }  

     UIGraphicsBeginImageContext(targetSize); 

     CGRect thumbnailRect = CGRectZero; 
     thumbnailRect.origin = thumbnailPoint; 
     thumbnailRect.size.width = scaledWidth; 
     thumbnailRect.size.height = scaledHeight; 

     [pickedImage drawInRect:thumbnailRect]; 

     self.selectedThumbnailImage = UIGraphicsGetImageFromCurrentImageContext(); 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"personChanged" object:nil]; 

     UIGraphicsEndImageContext(); 
    } 

    self.nameFirstString = (NSString *)ABRecordCopyValue(contact, kABPersonFirstNameProperty); 
    self.nameLastString = (NSString *)ABRecordCopyValue(contact, kABPersonLastNameProperty); 
    [nameFirstTextField setText:nameFirstString]; 
    [nameLastTextField setText:nameLastString]; 

    self.person.birthday = (NSDate *)ABRecordCopyValue(contact, kABPersonBirthdayProperty); 

    [self updateRightBarButtonItemState]; 
    [self dismissModalViewControllerAnimated:YES]; 

    return NO; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ 

    NSLog(@"%s", __FUNCTION__); 
    return NO; 
} 

- (void)saveImage { 

    // Delete any existing image. 
    NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext]; 
    Image *oldImage = person.image; 
    if (oldImage != nil) { 
     [context deleteObject:(NSManagedObject*)oldImage]; 
    } 

    // Create an image object for the new image. 
    UIImage *newImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context]; 
    [newImage setValue:selectedImage forKey:@"image"]; 
    [self.person setValue:newImage forKey:@"image"]; 
} 
- (IBAction)save:(id)sender { 

    NSLog(@"%s", __FUNCTION__); 

    [self.person setValue:selectedThumbnailImage forKey:@"thumbnailImage"]; 
    [self saveImage]; 

    self.person.nameFirst = nameFirstString; 
    self.person.nameLast = nameLastString; 

    NSError *error; 
    NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext]; 
    if (![context save:&error]) { 
     NSLog(@"AddPersonViewController - addViewControllerDidFinishWithSave - Person MOC save error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

    [self.delegate addPersonViewController:self didFinishWithSave:YES didEditPerson:person]; 
} 

編輯:我改變了saveImage代碼以下內容並得到了錯誤「非法嘗試建立關係‘之間的人’在不同的上下文對象:

- (void)saveImage { 

// Delete any existing image. 
//NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext]; 

NSLog(@"%s", __FUNCTION__); 

NSManagedObjectContext *savingPhotoContext = [[NSManagedObjectContext alloc] init]; 
self.savingPhotoMOC = savingPhotoContext;               
[savingPhotoMOC setPersistentStoreCoordinator:[[[UIApplication sharedDelegate] managedObjectContext] persistentStoreCoordinator]]; 
[savingPhotoContext release];  

Image *oldImage = person.image; 
if (oldImage != nil) { 
    [savingPhotoMOC deleteObject:(NSManagedObject*)oldImage]; 
} 

// Create an image object for the new image. 
UIImage *newImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:savingPhotoMOC]; 
[newImage setValue:selectedImage forKey:@"image"]; 
[newImage setValue:self.person forKey:@"person"]; 
//[self.person setValue:newImage forKey:@"image"]; 

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; 
[dnc addObserver:self selector:@selector(addControllerContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:savingPhotoMOC]; 

NSError *error; 
if (![savingPhotoMOC save:&error]) { 
    NSLog(@"Occasion View Controller - addViewControllerDidFinishWithSave - Adding MOC save error %@, %@", error, [error userInfo]); 
    exit(-1); // Fail 
} 
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:savingPhotoMOC]; 
self.savingPhotoMOC = nil; 

} - (無效)addControllerContextDidSave:(NSNotification *)saveNotification {

NSLog(@"%s", __FUNCTION__); 
NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext]; 
[context mergeChangesFromContextDidSaveNotification:saveNotification]; 

}

+0

如果需要很長時間,您應該在後臺執行此操作,並使用委託回調或nsnotification通知可能在保存完成時使用它的其他功能。 –

+0

行..我想這將涉及使用一個單獨的managedObjectContext?爲了簡化,我只使用一個MOC來完成我的整個應用程序。我確信這是一個糟糕的主意,但它對所有的上下文都有些困惑。我真的沒有看到需要多種背景,但現在我認爲我需要。謝謝。 – SAHM

+0

嗯,好吧,現在顯示我的經驗不足,但我發現我無法在一個上下文中保存新對象'person',同時在另一個上下文中保存對象'image'(並將其分配給'person')。我認爲這將工作,如果我合併在新的背景下,但:「非法企圖在不同背景下的對象之間建立關係」人「..不知道從哪裏去.. – SAHM

回答

1

如果需要很長時間,您應該在後臺執行此操作,並使用委託回調或nsnotification通知可能在保存完成時使用它的其他功能。你可以使用GCD或nsoperationqueue ...爲gcd看看'火熱機器人的博客。我喜歡它,因爲他解釋的事情不僅僅是提供複製粘貼的代碼。

+0

我打算把它作爲答案,因爲我知道這是正確的。然而,我個人很難得到它的工作,還沒有(見[這個鏈接](http://stackoverflow.com/questions/7693556/saving-large-image-with-core-data-and -gcd))。希望儘快開始工作。感謝您的正確方向。 – SAHM