0

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) => {
        if(haveAccess){
            contacts = addressBook.GetPeople().ToList();
        }
    });
}
  • Can you show the code that you are using to retrieve the `ABPerson`? As I can retrieve the thumbnail and full-size image on iOS 8 & 9 using the `ABPeoplePickerNavigationController` and returning an ABPerson and checking the `HasImage` and pulling the `NSData` via `GetImage`. – SushiHangover May 02 '16 at 17:59
  • I added code to the post. I did it according to [this](https://developer.xamarin.com/recipes/ios/shared_resources/contacts/find_a_contact/) tutorial. – Marko Savchuk May 02 '16 at 18:28

2 Answers2

1

in iOS9 those methods have been deprecated

instead, you should use CNContact ImageData and ImageDataAvailable

Overview of Contacts framework

Jason
  • 86,222
  • 15
  • 131
  • 146
1

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);
    }
}
addressBook.Dispose();

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

Output:

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
Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks a lot! I tried your code in new app and it worked. Then I started figure out what happans in my app and it turns out I lose somehow data, as I use method GetContacts and then used returning list. – Marko Savchuk May 02 '16 at 19:45