I use ABUnknownPersonViewController to display a contact view.
I try to set an image with:
NSData *dataRef = UIImagePNGRepresentation([UIImage imageNamed:@"contact3.png"]);
ABPersonSetImageData(newPersonViewController.displayedPerson, (CFDataRef)dataRef, nil);
It doesn’t work and I don’t know why. Any ideas?
You can’t just cast anNSDataobject to aCFDataRef; as noted in the docs, aCFDataRefis a “reference to an immutable CFData object”, which is not the same as anNSDatainstance:To create the
CFDataReffrom theNSDatainstance, you need to use theCFDataCreatemethod, passing the bytes and length:Note also that since you create the object yourself, you must also release it, following the Core Foundation Ownership Policy; you use the
CFReleasefunction to release ownership of the Core Foundation object:This is similar to the Memory Management in Cocoa, and once the retain count of the Core Foundation object reaches zero it will be deallocated.
Edit:
Stefanwas completely right, in his comment, thatNSDataandCFDataare also toll-free bridged on the iPhone with Cocoa-Touch as with Cocoa, so my original answer was wrong. My fault, should have edited it before.