使用ABPersonCopyImageData获取iOS联系人图像 [英] Get iOS contact image with ABPersonCopyImageData
问题描述
我正在尝试阅读我的iOS地址簿的联系人图片,但我遇到了一些问题。
I'm trying to read the contact image of my iOS addressbook but I'm having some troubles with it.
我有一个方法可以阅读ABAddressBookRef的联系人并将其放在结构上。
I have a method that read the information of contacts of ABAddressBookRef and put it on a struct.
func readContacts(addressBook: ABAddressBookRef){
let allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as NSArray
var numeros : [Numeros] = []
//lendo todos os contatos
for person: ABRecordRef in allPeople{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String
let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue() as! String
let phones : ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef
let data = ABPersonCopyImageData(person).takeRetainedValue() as NSData
let imagemContato = UIImage(data: data)
for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++) {
let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString
let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""
let cfStr:CFTypeRef = locLabel
let nsTypeString = cfStr as! NSString
let swiftString:String = nsTypeString as String
//let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
let numero: Numeros = Numeros(tipoNumero: swiftString, numero: phoneNumber as String)
numeros.append(numero)
}
let registroContato: Contato = Contato(primeiroNome: firstName, ultimoNome: lastName, contatoRecord: person, numeros: numeros, imagemContato: imagemContato!)
contatos.append(registroContato)
}
}
但是当我调试它时,它会停止:
but when I debug it, it stops on:
let data = ABPersonCopyImageData(person).takeRetainedValue() as NSData
并且不显示任何错误消息。
and doesn't show any error message.
我做错了什么?
我正在使用模拟器默认联系人来测试它,他们没有图像,但是占位符上有他/她名字的第一个字母。
I'm using the simulator default contacts to test it, they did not have images but have the placeholder with his/her first letter of the name.
已编辑:
我已将代码更改为
let imagemContato: UIImage?
if let data = ABPersonCopyImageData(person)?.takeRetainedValue() as? NSData {
print("\(firstName) with image")
imagemContato = UIImage(data: data)
}
else {
print("\(firstName) without image")
imagemContato = UIImage(named: "UooQo.jpg")
}
并且在控制台上所有联系人都没有图像:
and on the console all contacts have no image:
控制台输出:
Kate没有图像
Daniel没有图像
John没有图像
Anna没有图像
Hank没有图像
David没有图像
it显示没有联系人的图像,但我向约翰添加一个。
it shows no image for contact but i add one to John.
推荐答案
我之前在项目中遇到的同样问题。
试试这个,
Same problem that I faced in my project earlier. Try this,
//获取图片
let img = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail).takeRetainedValue()
//在你的图片中设置图片ImageView
//To set image in your ImageView
YourImageView.image=UIImage(data: img as! NSData)
这篇关于使用ABPersonCopyImageData获取iOS联系人图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!