13.9. Retrieving and Setting a Person’s Address Book Image
Problem
You want to be able to retrieve and set the images of address book entries.
Solution
Use one of the following functions:
ABPersonHasImageDataUse this function to find out if an address book entry has an image set.
ABPersonCopyImageDataUse this function to retrieve the image data (if any).
ABPersonSetImageDataUse this function to set the image data for an entry.
Discussion
As mentioned in this recipe’s Solution, we can use the ABPersonCopyImageData function to retrieve the
data associated with an image of a person entry in the address book. We
can use this function in a method of our own to make it more convenient
to use:
-(UIImage*)getPersonImage:(ABRecordRef)paramPerson{UIImage*result=nil;if(paramPerson==NULL){NSLog(@"The person is nil.");returnNULL;}NSData*imageData=(__bridge_transferNSData*)ABPersonCopyImageData(paramPerson);if(imageData!=nil){UIImage*image=[UIImageimageWithData:imageData];result=image;}returnresult;}
The ABPersonSetImageData
function sets the image data for a person entry in the address book.
Since this function uses data, not the image itself, we need to get
NSData from UIImage. If we want the data pertaining to a
PNG image, we can use the UIImagePNGRepresentation function to retrieve the PNG
NSData representation of the image of
type UIImage. To retrieve JPEG image data from an instance of UIImage, use the UIImageJPEGRepresentation function. Here is the method that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access