使用CNContactVCardSerialization从URI获取照片

时间:2019-07-09 06:08:50

标签: ios swift iphone vcard

我正在尝试使用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))")
}

我使用了https://tools.ietf.org/html/rfc2426#section-3.1.4

中的PHOTO归档示例

0 个答案:

没有答案