gpt4 book ai didi

swift - 将注释和图像添加到序列化的 VCF 联系人

转载 作者:行者123 更新时间:2023-11-30 10:50:17 25 4
gpt4 key购买 nike

我正在尝试使用 CNContactVCardSerialization 将联系人保存为 vcf,效果相对较好。我确实发现苹果不包含注释或图像作为 VCF 的一部分。我确实使用了 stackoverflow answer帮助序列化图像,但现在我也在努力添加注释。

这是我尝试过的:

extension CNContactVCardSerialization {
internal class func vcardDataAppendingPhoto(vcard: Data, photoAsBase64String photo: String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardPhoto = "PHOTO;TYPE=JPEG;ENCODING=BASE64:".appending(photo)
let vcardPhotoThenEnd = vcardPhoto.appending("\nEND:VCARD")
if let vcardPhotoAppended = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: vcardPhotoThenEnd) {
return vcardPhotoAppended.data(using: .utf8)
}
return nil

}
internal class func vcardDataAppendingNote(vcard: Data, note:String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardNote = "NOTE:".appending(note)
let vcardNoteThenEnd = vcardNote.appending("\nEND:VCARD")
if let vcardNoteAppended = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: vcardNoteThenEnd) {
return vcardNoteAppended.data(using: .utf8)
}
return nil
}

class func data(jpegPhotoContacts: [CNContact]) throws -> Data {
var overallData = Data()
for contact in jpegPhotoContacts {
let data = try CNContactVCardSerialization.data(with: [contact])
if (contact.note != "") || contact.imageDataAvailable {
if contact.imageDataAvailable {
if let base64imageString = contact.thumbnailImageData?.base64EncodedString(),
let updatedData = vcardDataAppendingPhoto(vcard: data, photoAsBase64String: base64imageString) {
overallData.append(updatedData)
}
}

if contact.note != ""{
if let updatedData = vcardDataAppendingNote(vcard: data, note: contact.note){
overallData.append(updatedData)
}
}
} else {
overallData.append(data)
}
}
return overallData
}
}

这似乎不起作用。我可以让笔记或图像部分工作,但不能同时工作。

我可能没有正确添加到 VCF 文件。

非常感谢任何帮助。

最佳答案

它并不漂亮,但这是我所做的以及目前的工作:

extension CNContactVCardSerialization {
internal class func vcardDataAppendingPhoto(vcard: Data, photoAsBase64String photo: String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardPhoto = "PHOTO;TYPE=JPEG;ENCODING=BASE64:".appending(photo)
if let vcardPhotoAppended = vcardAsString?.appending(vcardPhoto) {
return vcardPhotoAppended.data(using: .utf8)
}
return nil

}
internal class func vcardDataAppendingNote(vcard: Data, note:String) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardNote = "NOTE:".appending(note)
let vcardFinal = vcardNote.appending("\n")
if let vcardNoteAppended = vcardAsString?.appending(vcardFinal) {
return vcardNoteAppended.data(using: .utf8)
}
return nil
}

internal class func vcardDataAppendingEnd(vcard: Data) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)
let vcardEnd = "\nEND:VCARD"
if let vcardEndAppended = vcardAsString?.appending(vcardEnd) {
return vcardEndAppended.data(using: .utf8)
}
return nil
}

internal class func vcardDataCleanEnd(vcard: Data) -> Data? {
let vcardAsString = String(data: vcard, encoding: .utf8)

if let vcardCleaned = vcardAsString?.replacingOccurrences(of: "END:VCARD", with: "\n"){
return vcardCleaned.data(using: .utf8)
}
return nil
}

class func data(jpegPhotoContacts: [CNContact]) throws -> Data {
var overallData = Data()
for contact in jpegPhotoContacts {
let data = try CNContactVCardSerialization.data(with: [contact])
if (contact.note != "") || contact.imageDataAvailable {
if let updatedData = vcardDataCleanEnd(vcard: data){
overallData = updatedData
}
if contact.note != ""{
if let updatedData = vcardDataAppendingNote(vcard: overallData, note: contact.note){
overallData = updatedData
}
}
if contact.imageDataAvailable {
if let base64imageString = contact.thumbnailImageData?.base64EncodedString(),
let updatedData = vcardDataAppendingPhoto(vcard: overallData, photoAsBase64String: base64imageString) {
overallData = updatedData
}
}
if let updatedData = vcardDataAppendingEnd(vcard: overallData){
overallData = updatedData
}
} else {
overallData.append(data)
}
}
return overallData
}
}

关于swift - 将注释和图像添加到序列化的 VCF 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54739325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com