如何解决快速从firebase存储下载图像
图像位置为 gs://olio-ae400.appspot.com/Listings/Food/-M3g8pZDGmApicuaQtOi/MainImage
我想从这个 firebase 存储位置下载到 imageview。 用过下面的代码,但是无法从url下载图片?
let storage = Storage.storage()
var reference: StorageReference!
reference = storage.reference(forURL: "gs://olio-ae400.appspot.com/Listings/Food/-M3g8pZDGmApicuaQtOi/MainImage")
reference.downloadURL { (url,error) in
print("image url is",url!)
let data = NSData(contentsOf: url!)
let image = UIImage(data: data! as Data)
self.img.image = image
}
在响应中检索 url 时在 downloadURL 行出现错误。
解决方法
指定路径及其扩展名,然后使用:
let path = "Listings/Food/-M3g8pZDGmApicUAQtOi/MainImage.jpg"
let reference = Storage.storage().reference(withPath: path)
reference.getData(maxSize: (1 * 1024 * 1024)) { (data,error) in
if let err = error {
print(err)
} else {
if let image = data {
let myImage: UIImage! = UIImage(data: image)
// Use Image
}
}
}
,
安装 pod firebaseUI。
导入 FirebaseUI
从存储中获取引用并直接使用 SDwebimage 设置对 imageview 的引用,如下所示。
let ref2 = Storage.storage().reference(forURL: "gs://hungry-aaf15.appspot.com/Listings/Food/-MV04bNvewyGPHMYUkK9/MainImage")
cell.img.sd_setImage(with: ref2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。