我正在尝试使用CNContactVCardSerialization并读取PHOTO字段
如果照片是BASE64,则可以使用
System.NullReferenceException: 'Object reference not set to an instance of an object.'
输出
import UIKit
import Contacts
let str = "BEGIN:VCARD \n" +
"VERSION:3.0 \n" +
"N:Smith;John;Mr. \n" +
"PHOTO;ENCODING=b;TYPE=JPEG:VGVzdCBUZXN0IFRlc3QgVGVzdCBUZXN0 \n" +
"END:VCARD"
if let data = str.data(using: .utf8) {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first!
print("name: \(contact.givenName)")
print("surname: \(contact.familyName)")
print("photo: \(String(describing: contact.imageData))")
print("thumb: \(String(describing: contact.thumbnailImageData))")
}
但是,当照片是URI时,不是
name: John
surname: Smith
photo: Optional(24 bytes)
thumb: nil
输出
import UIKit
import Contacts
let str = "BEGIN:VCARD \n" +
"VERSION:3.0 \n" +
"N:Smith;John;Mr. \n" +
"PHOTO;VALUE=uri:http://www.example.com/pub/photos/jqpublic.gif \n" +
"END:VCARD"
if let data = str.data(using: .utf8) {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first!
print("name: \(contact.givenName)")
print("surname: \(contact.familyName)")
print("photo: \(String(describing: contact.imageData))")
print("thumb: \(String(describing: contact.thumbnailImageData))")
}
中的PHOTO归档示例