Get iOS Contact Image with Abpersoncopyimagedata

Get iOS contact image with ABPersonCopyImageData

Same problem that I faced in my project earlier.
Try this,

//To Get image

   let img = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail).takeRetainedValue()

//To set image in your ImageView

YourImageView.image=UIImage(data: img as! NSData)

Get Contact Image Data using ABPersonCopyImageData

I found out how to do this in ElCapitan:

import Contacts

func getContactImage(name:String) -> NSImage?
{
let store = CNContactStore()
do
{
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(name), keysToFetch:[CNContactImageDataKey])
if contacts.count > 0
{
if let image = contacts[0].imageData
{
return NSImage.init(data: image)
}
}
}
catch
{
}

return nil
}

iOS AddressBook - get contact image crash

check all for nil. if ABPersonCopyImageDataWithFormat returns nil, you call takeRetainedValue on nil. and then use it nil to create image too

guard let CFData = ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail) else {
print("no cfdata")
return
}

if let data = CFData.takeRetainedValue {
if let img = UIImage(data: data) {
image = img
}
}

iOS How to get contact image full screen

Just setup your UIImageView frame to be full screen. Then whatever picture you will display in it should be full screen. It will automatically get downsized for you.

Get image contact in address book on iOS

Yes it is completely possible, you simply ask each person record whether it has an image:

See the documentation:
http://developer.apple.com/library/ios/#documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/doc/uid/TP40007210

Relevant Functions:

ABPersonHasImageData(person)
ABPersonCopyImageDataWithFormat(person)

The ABPersonCopyImageDataWithFormat returns a CFDataRef.
Convert it to NSData with this (ARC) NSData* data = (__bridge_transfer NSData*) cfData;

An image can then be created with [UIImage imageWithData:data]

Get a Contact Picture ios

To get the FirstName and LastName , you can use :

NSString *firstName = (NSString *)ABRecordCopyValue(aABRecordRef, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(aABRecordRef, kABPersonLastNameProperty);

Here it shows how to Search By Number and Get the image using ABAddressBook.

But you want to search By FirstName and LastName. So According to the Documentation , you should use -recordsMatchingSearchElement: method for Multiple Arguments.

Once you get the matched Data , you can extract the image using below code :

CFDataRef imageData = ABPersonCopyImageData(aABRecordRef);
UIImage *image = [UIImage imageWithData:(NSData *)imageData];
CFRelease(imageData);

Hope you get something useful from this.

How to get the contact image URL from contacts?

I think you need to make a local copy of the data, and then save a refereance to that local copy in your database:

//create a fileName perhaps based on the contact name or a GUID
NSError *err;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);//find the cache dir. You might want to consider using the doc dir instead
NSString * path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:fileName];
[imgData writeToFile:path options:NSDataWritingAtomic error:&err];
if(!err)
{
///save path to the DB
}


Related Topics



Leave a reply



Submit