gpt4 book ai didi

swift - 获取联系人图像 - SwiftUI

转载 作者:行者123 更新时间:2023-12-04 08:42:11 26 4
gpt4 key购买 nike

我正在尝试获取给定联系人 的图像属性。这是我得到的:

型号

struct Contact: Identifiable {
let contact: CNContact

var id: String { contact.identifier }
var image: Data? { contact.imageData }
var givenName: String { contact.givenName }
}

static func fetch(_ completion: @escaping(Result<[Contact], Error>) -> Void) {
let containerID = CNContactStore().defaultContainerIdentifier
let predicate = CNContact.predicateForContactsInContainer(withIdentifier: containerID())

let keysToFetch = [
CNContactImageDataKey,
CNContactImageDataAvailableKey,
CNContactThumbnailImageDataKey,
CNContactGivenNameKey,
CNContactFamilyNameKey,
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey
] as [CNKeyDescriptor]

do {
let contacts = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
completion(.success(contacts.map({ .init(contact: $0)})))
} catch {
completion(.failure(error))
}
}

查看

struct ContactRow: View {
let contact: Contact

var body: some View {
VStack {
HStack {
Image(String(describing: contact.image))
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 50, height: 50)
.background(AppColor())
.clipShape(Circle())
.shadow(color: Color.black.opacity(0.1), radius: 1, x: 0, y: 1)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 10)

Text(contact.givenName)
.font(.headline)
.foregroundColor(Color(#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)))

}
}
}
}

我得到的错误是紫色的,然后说

runtime: SwiftUI: No image named 'Optional(38321 bytes)' found in asset catalog for /private/var/containers/Bundle/Application/ ...etc

显然,我在 Apple 提供的 Contacts 应用程序中有联系人图片。

有什么想法吗?

最佳答案

你的联系人图片是Data,所以你应该用不同的方式创建Image,比如

Image(uiImage: UIImage(data: contact.image ?? Data()) ?? UIImage())

关于swift - 获取联系人图像 - SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64510100/

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