Iphone ABPeoplePickerNavigationController保存较大的图像需要很长时间

Iphone ABPeoplePickerNavigationController保存较大的图像需要很长时间,iphone,image,save,filesize,abpeoplepickerview,Iphone,Image,Save,Filesize,Abpeoplepickerview,我使用ABPeoplePickerNavigationController将iPhone联系人导入我的应用程序。我最近决定,不只是保存缩略图,而是保存整个图像。有了iPad,可能需要不同裁剪的更大图像,我认为这是一条出路。但是,在导入后保存一些新联系人时,保存时间较长,具体取决于照片的大小。没有照片,保存是即时的,所以我知道这与照片的大小以及我正在使用它做什么有关。相关代码如下,以防任何人都能指出我做错了什么 - (BOOL)peoplePickerNavigationControlle

我使用ABPeoplePickerNavigationController将iPhone联系人导入我的应用程序。我最近决定,不只是保存缩略图,而是保存整个图像。有了iPad,可能需要不同裁剪的更大图像,我认为这是一条出路。但是,在导入后保存一些新联系人时,保存时间较长,具体取决于照片的大小。没有照片,保存是即时的,所以我知道这与照片的大小以及我正在使用它做什么有关。相关代码如下,以防任何人都能指出我做错了什么

    - (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];
}
}-(void)addControllerContextDidSave:(NSNotification*)saveNotification{

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

}

如果需要很长时间,则应在后台执行此操作,并在保存完成时使用委托回调或nsnotification通知可能正在使用它的其他函数。您可以使用GCD或nsoperationqueue…对于GCD,请查看“火焰机器人”的博客。我喜欢它,因为他解释了一些事情,而不仅仅是给出复制粘贴的代码。

如果需要很长时间,您应该在后台执行此操作,并在保存完成时使用委托回调或nsnotification通知可能正在使用它的其他函数。您可以使用GCD或nsoperationqueue…对于GCD,请查看“火焰机器人”的博客。我喜欢它,因为他解释了一些事情,而不仅仅是给出复制粘贴的代码。

如果需要很长时间,您应该在后台执行此操作,并在保存完成时使用委托回调或nsnotification通知可能正在使用它的其他函数。好的。。我想这需要使用一个单独的managedObjectContext?为了简化我的整个应用程序,我只使用一个MOC。。我肯定这是个坏主意,但它对所有的上下文都有点太混乱了。我真的不认为需要多个上下文,但现在我想我需要。谢谢。嗯,好吧,现在显示出我的经验不足,但我发现我无法在一个上下文中保存新对象“person”,而在另一个上下文中保存对象“image”(并将其分配给“person”)。我认为如果我合并到新的上下文中,这会起作用,但是:“非法尝试在不同上下文中的对象之间建立‘person’关系”。。不知道从这里去哪里…也许有人可以给我指一个简单线程的好教程?你可以使用GCD或nsoperationqueue…对于GCD,请看一下“Firey robot”的博客。我喜欢它,因为他不仅解释了复制粘贴的代码。如果需要很长时间,您应该在后台执行此操作,并在保存完成时使用委托回调或nsnotification通知可能正在使用它的其他函数。好的。。我想这需要使用一个单独的managedObjectContext?为了简化我的整个应用程序,我只使用一个MOC。。我肯定这是个坏主意,但它对所有的上下文都有点太混乱了。我真的不认为需要多个上下文,但现在我想我需要。谢谢。嗯,好吧,现在显示出我的经验不足,但我发现我无法在一个上下文中保存新对象“person”,而在另一个上下文中保存对象“image”(并将其分配给“person”)。我认为如果我合并到新的上下文中,这会起作用,但是:“非法尝试在不同上下文中的对象之间建立‘person’关系”。。不知道从这里去哪里…也许有人可以给我指一个简单线程的好教程?你可以使用GCD或nsoperationqueue…对于GCD,请看一下“Firey robot”的博客。我喜欢它,因为他解释了一些事情,而不仅仅是给出了复制粘贴的代码。我将把它作为答案,因为我知道它是正确的。然而,就我个人而言,我很难让它发挥作用,而且还没有(见)。希望它能很快工作。谢谢你指出了正确的方向。我将把这个作为答案,因为我知道它是正确的。然而,就我个人而言,我很难让它发挥作用,而且还没有(见)。希望它能很快工作。谢谢你指出了正确的方向。
NSLog(@"%s", __FUNCTION__);
NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext];
[context mergeChangesFromContextDidSaveNotification:saveNotification];