I'm trying to read the contact image of my iOS addressbook but I'm having some troubles with it.
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.
What am I doing wrong?
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.
EDITED:
i've change the code to
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:
console output:
Kate without image
Daniel without image
John without image
Anna without image
Hank without image
David without image
it shows no image for contact but i add one to John.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…