I need to convert my Objective-C to Swift to get an image of a contact from the Address Book. But I get an error with for cast from CFData
to NSData
if let imgData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize)?.takeUnretainedValue() {
if let image = UIImage(data: imgData) {
//Here's your UIImage
}
}
As explained in
Working with Cocoa Data Types, you have to convert the unmanaged object to a memory managed object with takeUnretainedValue()
or takeRetainedValue()
.
In your case
if (ABPersonHasImageData(person)) {
let imgData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize).takeRetainedValue()
let image = UIImage(data: imgData)
}
because ABPersonCopyImageDataWithFormat()
returns a (+1) retained value.