简体   繁体   English

Firebase 存储到 UIImageView

[英]Firebase Storage to UIImageView

I am trying to get an image that I have stored inside Firebase Storage inside an UIImageView.我正在尝试获取我存储在 UIImageView 中的 Firebase Storage 中的图像。 I have the URL of the image saved inside Firebase Database for that user, but don't know how to grab it我在 Firebase 数据库中为该用户保存了图像的 URL,但不知道如何获取它

you need to make an action for it this func opens the Photo carrier你需要为它做一个动作这个功能会打开照片载体

  @IBAction func selecFoto(_ sender: UIButton) {

        let imageController = UIImagePickerController()
            imageController.delegate = self
            imageController.sourceType = UIImagePickerController.SourceType.photoLibrary
//you can also set animate to false 
            self.present(imageController, animated: true, completion: nil)


        }

this func selects the image and you can storage wherever you want这个 func 选择图像,你可以存储在任何你想要的地方

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// here you can preview the image you selected if that's the case 
        imageView.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)


    }
import FirebaseStorage

Storage.storage().reference(forURL: yourDownloadedURL).getData(maxSize: 1048576, completion: { (data, error) in

    guard let imageData = data, error == nil else {
        return
    }
    someImageView.image = UIImage(data: imageData)

})

Obviously, handle errors as well as corrupted image data, by perhaps falling back to a default image (just because we have image data in hand does not guarantee it will render a perfect image), and use a maximum file size appropriate to your application.显然,处理错误和损坏的图像数据,可能会回退到默认图像(仅仅因为我们手头有图像数据并不能保证它会呈现完美的图像),并使用适合您的应用程序的最大文件大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM