Cover by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

11.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 people entries.

Solution

Use one of the following functions:

ABPersonHasImageData

Use this function to find out if an address book entry has an image set.

ABPersonCopyImageData

Use this function to retrieve the image data (if any).

ABPersonSetImageData

Use 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.");
        return NULL;
    }

    NSData *imageData = (__bridge_transfer NSData *)
    ABPersonCopyImageData(paramPerson);

    if (imageData != nil){
        UIImage *image = [UIImage imageWithData:imageData];
        result = image;
    }

    return result;

}

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required