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:
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 UIImagePNG
Representation
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 UIImage
JPEGRepresentation
function. Here is the method that ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.