您好我正在使用CNMutableContact()创建新联系人,并添加一些详细信息,如下所示。
let contact = CNMutableContact()
contact.givenName = “John”
let image = UIImage(named: "school")
let imgData = UIImagePNGRepresentation(image!)
contact.imageData = imgData
使用fallowing功能我通过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
{
}
}
}
在接收器设备中,联系人将保存到设备联系人,但不保存图像。 我做错了是阻止图像被保存?