Get image of a person from iPhone address book
Asked Answered
A

3

17

How do you get a person's image from an iPhone address book?

Achromatic answered 18/1, 2010 at 12:27 Comment(0)
A
42

You can do it like this....

NSData  *imgData = (NSData *)ABPersonCopyImageData(person);

UIImage  *img = [UIImage imageWithData:imgData];

where person is of type ABRecordRef. Now, as CFData and NSData are toll-free bridged, you can simply type cast CFData to NSData and get the image

Hope this helps.

Achromatic answered 18/1, 2010 at 12:50 Comment(4)
just a note that "imgaeWithData:imgData" in the last line of the code snippet should read "imageWithData:imgData". I fixed itZicarelli
Please note that this might not be the image that the users expects since it is not the cropped version of the image. See ChangUZ's answerMagnolia
@Magnolia But with out mentioning any image format is advantageous as while populating the images in cells of table view,we use image view as accessory view,because it automatically resizes the image and sets each image in unique format(same dimensions) :)Stenger
person is an ABRecordRef.Conceal
W
12
(NSData*)ABPersonCopyImageDataWithFormat([targetPeople objectAtIndex:index], kABPersonImageFormatThumbnail)

This is faster since it returns a thumbnail.

Wendiewendin answered 5/8, 2011 at 8:31 Comment(2)
+1 because in addition to being a thumbnail it can be that the contact's image was cropped so ChangUZ's approach would return the same image that is shown in the address book.Magnolia
But this method is available from iOS 4.1.Softpedal
D
0

Slightly refreshed code:

UIImage *image = nil;

@try
{
    CFDataRef cfImage = ABPersonCopyImageData(person);
    // or CFDataRef cfImage = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
    if (cfImage)
    {
        image = [UIImage imageWithData:(__bridge NSData*)cfImage];
        CFRelease(cfImage);
    }
}
@catch (NSException *exception)
{
    //...
}
Danyelledanyette answered 21/1, 2015 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.