gpt4 book ai didi

Swift:监听 url 属性然后下载照片

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

我在 viewDidLoad 中运行此代码,但 Profile.currentProfile 还没有 photoUrl,因此它为零,并且此代码永远不会运行

private func getProfilePicture() {
if let photoURLString = Profile.currentProfile?.photoUrl {
if let photoURL = URL(string: photoURLString) {
if let photoData = try? Data(contentsOf: photoURL) {
self.profilePhotoView.image = UIImage(data: photoData)
self.letsGoButton.isEnabled = true
}
}
} else {
self.profilePhotoView.image = UIImage(named: "default-profile-icon")
}
}

如何等到 photoUrl 不为零,然后运行此代码?谢谢

里克

(编辑)这就是配置文件的设置方式。这在 viewController 实例化之前调用。

func copyProfileFieldsFromFB(completionHandler: @escaping ((Error?) -> Void)) {
guard AccessToken.current != nil else { return }
let request = GraphRequest(graphPath: "me",
parameters: ["fields": "email,first_name,last_name,gender, picture.width(480).height(480)"])
request.start(completionHandler: { (_, result, error) in
if let data = result as? [String: Any] {
if let firstName = data["first_name"] {
Profile.currentProfile?.firstName = firstName as? String
}
if let lastName = data["last_name"] {
Profile.currentProfile?.lastName = lastName as? String
}
if let email = data["email"] {
Profile.currentProfile?.email = email as? String
}
if let picture = data["picture"] as? [String: Any] {
if let imageData = picture["data"] as? [String: Any] {
if let url = imageData["url"] as? String {
Profile.currentProfile?.photoUrl = url
}
}

}
}
completionHandler(error)
})
}

最佳答案

通常,您需要使用完成处理程序来跟踪异步事件。所以在你的 viewDidLoad() 中你可以调用类似的东西

    Profile.currentProfile?.getPhotoURL { urlString in
if let photoURL = URL(string: photoURLString) {
if let photoData = try? Data(contentsOf: photoURL) {
self.profilePhotoView.image = UIImage(data: photoData)
self.letsGoButton.isEnabled = true
}
}
}

在你的 Profile 类上,它看起来像这样:

 func getPhotoURL(completion: @escaping (urlString) -> Void) {
// get urlString
completion(urlString)
}

关于Swift:监听 url 属性然后下载照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438099/

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