[英]ABPeoplePickerNavigationController saving larger image takes a LONG time
我使用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];
}
編輯:我將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;
}-(void)addControllerContextDidSave:(NSNotification *)saveNotification {
NSLog(@"%s", __FUNCTION__);
NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext];
[context mergeChangesFromContextDidSaveNotification:saveNotification];
}
如果需要很長時間,則應在后台執行此操作,並在保存完成后使用委托回調或nsnotification通知可能正在使用它的其他函數。 您可以使用GCD或nsoperationqueue ...有關gcd的信息,請訪問“火熱的機器人”博客。 我喜歡它,因為他解釋的不僅是提供復制粘貼的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.