我正在尝试从地址簿中检索用户联系信息并加载到 Collection View 单元格中。 我检索用户名和电话没有任何问题,但我不知道如何将图像加载到 Collection View
用户名有效,电话有效。 下面是检索用户名和用户电话的函数。
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
// You can fetch selected name and number in the following way
// user name
let userName:String = contact.givenName
// user phone number
let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value
// let userImage =
// user phone number string
let primaryPhoneNumberStr:String = firstPhoneNumber.stringValue
// print(primaryPhoneNumberStr)
// print(userName)
save(name: userName, nameKey: "name", phone: primaryPhoneNumberStr, phoneKey: "phone")
}
谁能帮我用这个函数加载图片吗?
最佳答案
检查图像是否可用并将其分配给常量。
if contact.imageDataAvailable {
let yourImage = UIImage(data: contact.imageData)
}
https://developer.apple.com/documentation/contacts/cncontact
关于swift - 将地址簿联系人图像加载到 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57457771/