StorageReference to URL



我正在努力遵循Firebase的文档,了解如何在应用程序中向用户显示Firebase存储中的图像。这是下面的文件:

// Reference to an image file in Firebase Storage
let reference = storageRef.child("images/stars.jpg")
// UIImageView in your ViewController
let imageView: UIImageView = self.imageView
// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")
// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)

我试着自己做这件事:

//get image url from deal
self.ref.child("deals").child("log").child(currentDealView).child("pictures").child(currentImageView).child("imageSelected").child("imageSelected").observeSingleEvent(of: .value) { (snapshot) in
let imageUrlString = snapshot.value as! String
let imageUrlUrl = URL(string: imageUrlString)
print(imageUrlString)
//prints "deals/log/dealNumber83/pictures/googleScreenshot/imageSelected"
// Reference to an image file in Firebase Storage
let reference = self.storageRef.child(imageUrlString)
// UIImageView in your ViewController
let viewPicImageView: UIImageView = self.viewPicImageView
// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")
// Load the image using SDWebImage
viewPicImageView.sd_setImage(with: reference, placeholderImage: placeholderImage)

但在最后一行,我在"with:reference"处出现错误,说:"无法将'StorageReference'类型的值转换为预期的参数类型'URL?'"我在网上找不到任何关于如何将StorageReference转换为URL的信息?谢谢

您可以使用这个:

let Ref = Storage.storage().reference(forURL: imageUrlUrl)
Ref.getData(maxSize: 1 * 1024 * 1024) { data, error in
if error != nil {
print("Error: Image could not download!")
} else {
yourImageView.image = UIImage(data: data!)
}
}

希望它能帮助。。。

或者,FirebaseStorageSwift测试版可以利用Swift Result类型。(将pod 'FirebaseStorageSwift'添加到Podfile。(

let Ref = Storage.storage().reference(forURL: imageUrlUrl)
Ref.getData(maxSize: 1 * 1024 * 1024) { result in
switch result {
case let .success(data):
yourImageView.image = UIImage(data: data)
case let .failure(error):
print("Error: Image could not download! (error)")
}
}

相关内容

  • 没有找到相关文章

最新更新