2010-07-25 85 views
0

我坚持,不知道该怎么办。苹果文档似乎没有涵盖这一点。ABPersonSetImageData工作,如果没有图像,什么也不做,如果图像存在

我的代码:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{ 
NSDictionary *info = [self selectedProjectInfo]; 

ABAddressBookRef addressBook = ABAddressBookCreate(); 

CFErrorRef error = nil; 

if (ABPersonSetImageData(person, (CFDataRef)(UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:[info objectForKey:@"ImagePath"]], 1.0f)), &error)) 
{ 
    ABAddressBookAddRecord(addressBook, person, &error); 

    NSLog(@"Set contact photo %@", error); 
    if (ABAddressBookHasUnsavedChanges(addressBook)) 
    { 
     NSLog(@"Changes made to address book"); 
    } 
    else { 
     NSLog(@"No changes made to address book"); 
    } 

    if (ABAddressBookSave(addressBook, &error)) 
    { 
     NSLog(@"Saved"); 
     UIAlertView *contactSuccessAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [contactSuccessAlert show]; 
     [contactSuccessAlert release]; 
    } 
    else { 
     NSLog(@"Not saved"); 
    } 
} 
else { 
    NSLog(@"Error saving contact photo %@", error); 
} 
CFRelease(addressBook); 

[self.navigationController dismissModalViewControllerAnimated:YES]; 
return NO; 

}

它工作正常,是否有接触没有现有的图像,但如果已经存在它不被替换。有谁知道我该怎么办?

提前感谢

+0

也刚刚注意到,如果我设置图像不起作用,删除它在联系人应用程序,然后尝试从我的应用程序再次设置!嗯... – Marky 2010-07-25 15:11:42

回答

0

嗨只需按照它的解决方案的链接, click here

我希望它会帮助你

+0

嗨,谢谢你的回答,但这并不能帮助我。如果某个联系人没有(并且从来不存在)联系人,我可以毫无问题地添加图片。问题是如果存在现有图像或图像(即通过编辑地址簿应用程序中的联系人删除)。 – Marky 2010-07-27 15:59:51

相关问题