Firebase 存储检索图像
Firebase Storage retrieval images
我正在努力了解 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 框架中,为您提供了下载图像的便捷方法,即它为您提供了指定图像大小的选项。这可能针对获取图像进行了优化,并且在大多数情况下是首选方法。
我正在努力了解 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 框架中,为您提供了下载图像的便捷方法,即它为您提供了指定图像大小的选项。这可能针对获取图像进行了优化,并且在大多数情况下是首选方法。