嗨,我正在使用 CNMutableContact() 创建新联系人并添加一些细节,如下所示。
let contact = CNMutableContact()
contact.givenName = “John”
let image = UIImage(named: "school")
let imgData = UIImagePNGRepresentation(image!)
contact.imageData = imgData
使用休闲功能,我通过 Airdrop 将联系人共享到任何其他设备。
func shareContacts(contacts: [CNMutableContact]) throws {
guard let directoryURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
return
}
var filename = NSUUID().uuidString
if let contact = contacts.first, contacts.count == 1 {
if let fullname = CNContactFormatter().string(from: contact) {
filename = fullname.components(separatedBy: "").joined(separator: "")
}
}
let fileURL = directoryURL.appendingPathComponent(filename).appendingPathExtension("vcf")
let data = try CNContactVCardSerialization.data(with: contacts)
print("filename: \(filename)")
print("contact: \(String(data: data, encoding: String.Encoding.utf8))")
try data.write(to: fileURL, options: .atomicWrite)
let activityViewController = UIActivityViewController(
activityItems: [fileURL],
applicationActivities: nil
)
present(activityViewController, animated: true, completion: nil)
activityViewController.completionWithItemsHandler = { (activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) -> Void in
if completed == true {
print("Saved")
}
else
{
}
}
}
在接收器设备中,联系人正在保存到设备联系人中,但图像未保存。我做错了什么阻止图像被保存?