iOS can't get person's image

此生再无相见时 提交于 2019-12-04 11:36:04

I may be too late for a solution for you, but maybe this will help others that are stuck with the same problem. Looks like ABPersonHasImageData() and ABPersonCopyImageDataWithFormat() don't work as expected on ABRecordRef copies (e.g. an ABContactRef from an array obtained using ABAddressBookCopyArrayOfAllPeople()), at leas on iOS 5.x. You may workaround it like this:

- (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;
}
Nikhil Jindal

Any further update on this ?

I have received complaints for some user not able to see thumbnail for a few contacts. Mostly it works fine, Is there any special case in which thumbnails aren't returned.

I am using the following piece of code:

 - (instancetype)initWithABContact:(ABRecordRef)contact {
          NSData *iThumbnailData = nil;
          if (ABPersonHasImageData(contact)) {
            iThumbnailData =
            CFBridgingRelease(ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail));
          }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!