使用FirebaseStorage关闭(UIImage→URL) [英] Closure with FirebaseStorage (UIImage→URL)

查看:63
本文介绍了使用FirebaseStorage关闭(UIImage→URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase身份验证/存储/Firestore创建新的注册过程.

I am creating a new registration process using Firebase Auth / Storage / Firestore.

这是新注册的过程(首先通过Auth进行身份验证,在Firestore中注册返回的用户,如果有图片则保存URL).

Here is the process of new registration (first authenticate with Auth, register the returned User in Firestore, save the URL if there is an image) process.

static func signUp(name: String, email: String, password: String, image: UIImage?, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
    Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
        if error != nil {
            onError(error)
            return
        }
        guard let uid = user?.user.uid else { return }
        var dict: [String: Any] = [
            "name": name,
            "email": email
        ]
        // If Image is Set
        if let image = image {
            StorageService.storage(image: image, path: .icon, id: uid) { (imageUrl) in
                dict["iconUrl"] = imageUrl
            }
        }
        Firestore.firestore().collection("users").document(uid).setData(dict) { (error) in
            if let error = error {
                print(error)
                return
            }
        }
        onSuccess()
    })
}

以下是将Storage UIImage作为参数并返回URL的功能类StorageService {

The following is a function of taking the Storage UIImage as an argument and returning the URL      class StorageService {

// Upload Image to Storage
static func storage(image: UIImage?, path: PathType, id: String, completion: @escaping (_ imageUrl: String?) -> ()) {
    guard let image = image, let imageData = UIImageJPEGRepresentation(image, 0.1) else {
        print("Non Image")
        completion(nil)
        return
    }
    let storageRef = Storage.storage().reference().child(path.rawValue).child(id)
    storageRef.putData(imageData, metadata: nil, completion: { (metaData, error) in
        if let error = error {
            print("Fail to Put Data in Storage : \(error)")
            completion(nil)
            return
        }
        storageRef.downloadURL { (imageUrl, error) in
            if let error = error {
                print("Fail to Download Url : \(error)")
                completion(nil)
                return
            }
            if let imageUrl = imageUrl?.absoluteString {
                completion(imageUrl)
            }
        }
    })
}

}

成功注册Auth并将其保存到FireStore,但是当有映像时,尽管它存储在存储中,但图像的URL并未保存在Firestore中.

Registration of Auth and saving to FireStore are successful, but when there is an image, Although it is stored in Storage, the URL of the image is not saved in the Firestore.

storage()如何编写闭包?

storage () Is there a problem with how to write a closure?

推荐答案

函数 StorageService.storage 是异步的,当有映像时,执行插入消防站的函数而未收到URL响应.您必须将函数插入到 StorageService.storage 的closure中,以获取并保存图像的URL

The function StorageService.storage is asynchronous, when there is an image, the function to insert in the firestore is executed without receiving the URL response. You must place your function to insert in the clousure of StorageService.storage to get and save the URL of the image

// If Image is Set
    if let image = image {
      StorageService.storage(image: image, path: .icon, id: uid) { (imageUrl) in
          dict["iconUrl"] = imageUrl
          Firestore.firestore().collection("users").document(uid).setData(dict) { (error) in
              if let error = error {
                  print(error)
                  return
              }
              onSuccess()
          }
      }
    }else {
      Firestore.firestore().collection("users").document(uid).setData(dict) { (error) in
          if let error = error {
              print(error)
              return
          }
          onSuccess()
      }
    }

这篇关于使用FirebaseStorage关闭(UIImage→URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆