2013-09-16 47 views
1

我使用ABPeoplePickerNavigationController來表示聯繫人的表示。通過點擊聯繫我需要爲它設置新的圖像。我添加了代碼來更改人員數據,但無法更改圖像。有什麼建議麼?爲聯繫人iOS設置圖像

這是我下面的代碼:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    NSData *dataRef = UIImagePNGRepresentation(self.theImage); 
    CFDataRef cfdata = CFDataCreate(NULL, [dataRef bytes], [dataRef length]); 
    CFErrorRef error; 

    ABPersonRemoveImageData(person, &error); // clean any image first from ref 
    if (ABAddressBookSave(_addressBook, &error)) 
    { 
     ABPersonSetImageData(person, cfdata, &error); 
     ABAddressBookSave(_addressBook, &error); 
    } 
    CFRelease(cfdata); 

    [self dismissViewControllerAnimated:YES completion:nil]; 

    return NO; 
} 

我下載的示例從這裏:link

要檢查它是如何工作的,你可以下載代碼並修改委託下面我的代碼。

回答

1

,你需要通過ABAddressBookSave()

而且保存更改,請記住,如果您的聯繫還沒有一個圖像,當您使用ABPersonSetImageData既縮略圖和全尺寸圖像將被添加。但是,如果您的聯繫人已經有全尺寸圖像,只有當您使用ABPersonSetImageData時,將會設置縮略圖。

// this is not production level code. method call return values and errors 
// need to be handled properly 
ABPersonRemoveImageData(person, &error); // clean any image first from ref 
if (ABAddressBookSave(addressBook, &error)) 
{ 
    ABPersonSetImageData(person, cfdata, &error); 
    ABAddressBookSave(addressBook, &error) 
} 
+0

感謝您的意見我已根據您的意見更新了我的問題代碼,但我仍然沒有看到該圖像出現在我的聯繫人中。我檢查cfdata,人,_addressBook他們都有價值 –

+0

我已經添加鏈接到我從哪裏下載它的來源 –

+1

我想我已經使用了不同的地址簿參考。我從ABPeoplePickerNavigationController獲得地址簿實例,並且效果很好。謝謝 –

相關問題