2012-04-15 53 views
4

我有兩個tableViewControllers。第一個有一個聯繫人列表。另一個顯示詳細的人的信息。iOS無法獲取人物圖像

的第一tableViewController

ABAddressBookRef addressBook = ABAddressBookCreate(); 
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook); 
NSArray *allPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source,kABPersonSortByFirstName); 
for (int i = 0; i < [allPeople count]; i++) 
{ 
    ... 
    contactClass = [[ContactClass alloc] initWithName:name surName:surName manID:[allPeople objectAtIndex:i]]; 
    ... 
} 

的第二tableViewController的代碼

ABRecordRef person = (__bridge ABRecordRef)contactClass.manID; 
BOOL isHasImage = ABPersonHasImageData(person); 

可變isHasImage甲chunck的代碼塊是總是假的,即使接觸具有的化身。我甚至在第一個tableViewController上檢查了這個,如果person有一個頭像,那麼它會返回true和image。

有誰知道我爲什麼無法獲取聯繫人圖片?

p.s. contactClass.manIDid的類型。它有一個正確的地址,因爲ABMultiValueRef multiValue = ABRecordCopyValue((__bridge ABRecordRef)contactClass.manID, kABPersonPhoneProperty);返回正確的值在第二個tableViewController

回答

10

我可能爲你太晚瞭解決方案,但也許這將幫助其他困住相同問題的其他人。 看起來像ABPersonHasImageData()ABPersonCopyImageDataWithFormat()ABRecordRef副本(例如,使用ABAddressBookCopyArrayOfAllPeople()獲得的陣列中的ABContactRef)不能如預期的那樣工作,在iOS 5.x版本中仍然如此。你可以這樣解決:

- (UIImage*)imageForContact: (ABRecordRef)contactRef { 
    UIImage *img = nil; 

    // can't get image from a ABRecordRef copy 
    ABRecordID contactID = ABRecordGetRecordID(contactRef); 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 

    ABRecordRef origContactRef = ABAddressBookGetPersonWithRecordID(addressBook, contactID); 

    if (ABPersonHasImageData(origContactRef)) { 
     NSData *imgData = (NSData*)ABPersonCopyImageDataWithFormat(origContactRef, kABPersonImageFormatOriginalSize); 
     img = [UIImage imageWithData: imgData]; 

     [imgData release]; 
    } 

    CFRelease(addressBook); 

    return img; 
} 
0

對此有何進一步更新?

我收到了一些用戶無法看到幾個聯繫人縮略圖的投訴。大多數情況下,它工作正常,是否有任何特殊情況下不會返回縮略圖。

我使用下面的代碼:

- (instancetype)initWithABContact:(ABRecordRef)contact { 
      NSData *iThumbnailData = nil; 
      if (ABPersonHasImageData(contact)) { 
      iThumbnailData = 
      CFBridgingRelease(ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail)); 
      } 
    } 
+0

你認真問了一個問題,答案??? – 2016-03-14 07:41:53