Marko Savchuk Marko Savchuk - 10 months ago 60
iOS Question

Can not get image from ABPerson from contacts

Using Xamarin I am trying to get image from ABPerson from AddressBook. I am successfuly getting all info about person name, phones etc. But method ABPerson.HasImage returns false for all contacts even for those who have a photo in the address book and method ABPerson.GetImage also always returns null. I tried to test it not on the simulator only, but also on the device. Anyone know how I can fix it?

Code that I am using to to retrieve contacts:

using (var addressBook = new ABAddressBook()){
addressBook.requestAccess((bool haveAccess, NSError e) => {
contacts = addressBook.GetPeople().ToList();


This example is just grabbing the default Contacts from simulator that have images that I applied to a few of them.

Try this code in replacement of yours and check the application output to see if the ABPerson's that have images show up properly.

Sample code:

var addressBook = new ABAddressBook();
var contacts = addressBook.GetPeople();
foreach (var contact in contacts)
    D.WriteLine("{0} {1}", contact.FirstName, contact.LastName);
    D.WriteLine("Has image?: {0}", contact.HasImage);
    if (contact.HasImage)
        var thumb = contact.GetImage(ABPersonImageFormat.Thumbnail);
        var full = contact.GetImage(ABPersonImageFormat.OriginalSize);
        var thumbsize = thumb.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
        var fullsize = full.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
        D.WriteLine("Thumb length: {0}", thumbsize.Length);
        D.WriteLine("Full length: {0}", fullsize.Length);

Note: D is using D = System.Diagnostics.Debug;


Kate Bell
Has image?: False
Daniel Higgins
Has image?: True
Thumb length: 35988
Full length: 196492
John Appleseed
Has image?: True
Thumb length: 59180
Full length: 1691176
Anna Haro
Has image?: False
Hank Zakroff
Has image?: True
Thumb length: 50364
Full length: 3473024
David Taylor
Has image?: False