How do you get a person's image from an iPhone address book?
Get image of a person from iPhone address book
Asked Answered
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.
just a note that "imgaeWithData:imgData" in the last line of the code snippet should read "imageWithData:imgData". I fixed it –
Zicarelli
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 answer –
Magnolia
@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
(NSData*)ABPersonCopyImageDataWithFormat([targetPeople objectAtIndex:index], kABPersonImageFormatThumbnail)
This is faster since it returns a thumbnail.
+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
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)
{
//...
}
© 2022 - 2024 — McMap. All rights reserved.