2010-07-25 26 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

相關問題