使用Xamarin我试图从AddressBook中获取ABPerson的图像。我成功地获得了有关人名,电话等的所有信息。但方法ABPerson.HasImage 对所有联系人都返回false,即使对于那些在地址簿中有照片的人而言,方法ABPerson.GetImage 也始终返回null。我试图仅在模拟器上测试它,但也在设备上测试它。有谁知道我怎么解决它?
我用来检索联系人的代码:
using (var addressBook = new ABAddressBook()){
addressBook.requestAccess((bool haveAccess, NSError e) => {
if(haveAccess){
contacts = addressBook.GetPeople().ToList();
}
});
}
答案 0 :(得分:1)
答案 1 :(得分:1)
这个例子只是从模拟器中获取默认的联系人,这些联系人包含我应用于其中几个的图像。
请尝试使用此代码替换您的代码并检查应用程序输出,以查看具有图像的ABPerson
是否正确显示。
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();
注意:D
为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