gpt4 book ai didi

ios - 如何在 iOS Swift 中显示来自新 Firebase 的个人资料图片?

转载 作者:行者123 更新时间:2023-11-29 00:49:03 25 4
gpt4 key购买 nike

新的 Firebase 允许您为用户提供 NSURL 个人资料图像属性,但我在显示它时遇到了问题。这是我保存它的方法......

let user = FIRAuth.auth()?.currentUser
if let user = user {
let changeRequest = user.profileChangeRequest()
changeRequest.photoURL = searchURL
changeRequest.commitChangesWithCompletion { error in
if let _ = error {
print("Try Again")
} else {
print("Photo Updated")
self.profileImage.image = image
}
}
}

这就是我试图检索它的方式......

if let user = FIRAuth.auth()?.currentUser
{
let name = user.displayName
let pic = user.photoURL
self.displayNameLBL.text = name
if pic != nil
{
print(pic!)
let urlString: String = pic!.path!
self.profileImage.image = UIImage(named: urlString)
//self.profileImage.image = UIImage(data: pic! as NSURL)
}else
{
self.profileImage.image = UIImage(named: "imagePlaceholder")
}
}

我得到了user.displayName,但没有得到图像。

最佳答案

您正在尝试显示 URL,而不是图像,您需要先下载图像,然后才能使用此扩展程序:

 extension UIImageView {
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .ScaleAspectFit) {
guard let url = NSURL(string: link) else { return }
contentMode = mode
NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
let data = data where error == nil,
let image = UIImage(data: data)
else { return }
dispatch_sync(dispatch_get_main_queue()) {
self.image = image
}
}.resume()
}
}

用法:

imageView.downloadedFrom(stringURL, contentMode: .ScaleAspectFill)

关于ios - 如何在 iOS Swift 中显示来自新 Firebase 的个人资料图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318393/

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