Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image of contacts is not fetching

I am using this code to fetch contact image from device but its not printing any output.

if contact.isKeyAvailable(CNContactImageDataKey) {
    if let contactImageData = contact.thumbnailImageData {
        print("image \(String(describing: UIImage(data: contactImageData)))")
    } else {
        print("No image available")
    }
} else {
    print("No Key image available")
}

but it is printing only "No Key image available" though some of my contacts have images . i tried imageData instead of thumbnailImageData but showing same results.

like image 275
Mitu Vinci Avatar asked Apr 19 '17 09:04

Mitu Vinci


2 Answers

Make sure CNContactImageDataAvailableKey and CNContactThumbnailImageDataKey is contained in your keysToFetch and remove the isKeyAvailable if clause:

if let imageData = contact.thumbnailImageData {
    print("image \(String(describing: UIImage(data: imageData)))")
} else {
    print("No image available")
}
like image 156
shallowThought Avatar answered Sep 28 '22 02:09

shallowThought


Add Keys to your fetch request (image keys missing)

 let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactImageDataAvailableKey, CNContactThumbnailImageDataKey]
 let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
like image 40
user3826696 Avatar answered Sep 28 '22 00:09

user3826696