gpt4 book ai didi

swift - Firebase 存储检索图像

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

我正在尝试了解 Firebase 存储。我在网上看到并尝试了两种获取图像的方法。这两者有什么区别? (两者都有效)。

因此,当我从 Firebase 数据库获取 photoUrl 后:

1.

if let data = NSData(contentsOfURL: NSURL(string:photoUrl)!)
{
let myImage = UIImage(data: data)!

MyImageCache.sharedCache.setObject(myImage, forKey: self.key)
//etc
}

2.

 self.storage.referenceForURL(photoUrl).dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil)
{
print(error)
}
else
{
let myImage = UIImage(data: data!)

MyImageCache.sharedCache.setObject(myImage!, forKey: self.key)
//etc
}
}

最佳答案

关于第一种方法,您不应该将其用于网络调用。来自文档:

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated. Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSURLSession class. See URL Session Programming Guide for details.

第二种方法内置于 Firebase 框架中,为您提供下载图像的便捷方法,即它为您提供指定图像大小的选项。这可能针对获取图像进行了优化,并且在大多数情况下是首选方法。

关于swift - Firebase 存储检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876728/

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