如何快速存储在Firebase中上传的图像

问题描述 投票:0回答:2

[嗨,我如何下载在firebaseStorage上上传的图像?这是我要发送给firebase的字典:

@IBAction func uploadEvent(_ sender: Any) {
            let ref = Database.database().reference(withPath: "events").childByAutoId()
            let dictEvent: [String: Any] = [
                "content": description,
                "endTime": timeEnd,
                "idOrganizer": Auth.auth().currentUser!.uid,
                "image": "",
                "name": name,
                "price":price,
                "startDate": date,
                "endDate": timeEnd,
                "typeEvent":eventTypeChoice,
                "typePlace":eventPlaceChoice,
                "address": address,
                "period": period]

            ref.setValue(dictEvent) {
              (error:Error?, ref:DatabaseReference) in
              if let error = error {
                print("Data could not be saved: \(error).")
              } else {
                print("Data saved successfully!")
              }
            }

            dismiss(animated: true, completion: nil)

        }

我知道下载图像的功能是:

  func upload(with folder: String, completion: @escaping (URL?) -> Void) {
        let metadata = StorageMetadata()
        metadata.contentType = "image/jpeg"

        //let fileName = [UUID().uuidString, String(Date().timeIntervalSince1970)].joined()
        guard let data = self.jpegData(compressionQuality: 0.4) else { return }
        let storage = Storage.storage().reference()
        storage.child(folder).putData(data, metadata: metadata) { meta, error in
            if let error = error {
                print(error)
                completion(nil)
                return
            }

            storage.child(folder).downloadURL { url, error in
                if let error = error {
                    // Handle any errors
                    print(error)
                    completion(nil)
                }
                else {
                    completion(url)
                }
            }
        }
    }

但是我只能打印该网址,而不发送它。问题是我不知道如何访问该URL,然后将其传递给我的"image"字段?有人可以帮我吗?

swift firebase firebase-realtime-database firebase-storage
2个回答
0
投票

您可以观看此tutorial,向您展示如何将图像上传到FirebaseStorage,并阅读Firebase documentation


0
投票

您可以使用翠鸟来缓存下载的图像。

var iView:UIImageView!

self.iView = UIImageView.init(image:UIImage(named:“ blabla”))//在viewDidLoad上执行此操作

func cacheImages(){
    let ref = Storage.storage().reference()
    for i in 0..<yourImageArray.count{
        ref.child("YourStorageFolder/\(yourImageArray[i]).png").downloadURL { url, error in
        if error != nil{
            print("ERROR",error?.localizedDescription as Any)
        } else{
            print(url!)
            let resource = ImageResource(downloadURL: url!, cacheKey: "\(yourImageArray[i])")
            self.iView.kf.setImage(with: resource)
        }
    }
 }    

}

您可以使用以下方式检索图像:

     self.cache.retrieveImage(forKey: (value?["abc"] as! String)) { result in
             switch result{
                    case .success(let value2):
                        print(value2.image as Any)
                    DispatchQueue.main.async {
                    yourImaveView.image = value2.image
                    }
                    case .failure(let error):
                        print(error)
                    }
                 }

希望有帮助...

© www.soinside.com 2019 - 2024. All rights reserved.