【问题标题】:How to save a group (array) of images into firebase cloud storage?如何将一组(数组)图像保存到 Firebase 云存储中?
【发布时间】:2019-01-23 19:21:18
【问题描述】:

我正在尝试将用户拍摄的一组图像保存到云存储中,然后在该个人资料下保存该图像的 URL。我怎样才能做到这一点?目前,我编写了下面的代码,该代码一次保存用户拍摄的一张图像,但有人告诉我这是一种不好的做法。此外,不会添加图像,而是替换之前的图像。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // let imageToUse = PhotoArray()
        // let data = UIImagePNGRepresentation(userPickedImage) //here convert to data
       PhotoArray.sharedInstance.photosArray.append(userPickedImage)  //append converted data in array

        //                do {
        //                    try realm.write {
        //                        realm.add(imageToUse)
        //                    }
        //                } catch {
        //                    print(“error adding image to array\(error)“)
        //                }

            imageView.image = userPickedImage
//-----------------------------//
            //Create a reference to the image
            let imageRef = Storage.storage().reference().child("image.jpg")

            // Get image data
            if let uploadData = UIImagePNGRepresentation(userPickedImage) {

                // Upload image to Firebase Cloud Storage
                imageRef.putData(uploadData, metadata: nil) { (metadata, error) in
                    guard error == nil else {
                        // Handle error
                        return
                    }
                    // Get full image url
                    imageRef.downloadURL { (url, error) in
                        guard let downloadURL = url else {
                            // Handle error
                            return
                        }

                        // Save url to database
                        Firestore.firestore().collection("images").document("myImage").setData(["imageUrl" : downloadURL.absoluteString])
                    }
                }
            }
//-----------------------------//
        }
    // print(PhotoArray().photosArray.count)
    imagePicker.dismiss(animated: true, completion: nil)
}

【问题讨论】:

    标签: ios arrays swift firebase firebase-storage


    【解决方案1】:

    我在 firebase 文档中找到了这个:

    // Add a new document with a generated id.
    var ref: DocumentReference? = nil
    ref = db.collection("cities").addDocument(data: [
    "name": "Tokyo",
    "country": "Japan"
    ]) { err in
    if let err = err {
        print("Error adding document: \(err)")
    } else {
        print("Document added with ID: \(ref!.documentID)")
    }
    }
    

    希望这会对您有所帮助。 你可以在这里阅读:Click me

    【讨论】:

    • 这应该在哪里实现
    猜你喜欢
    • 2019-01-22
    • 2016-11-08
    • 2020-04-26
    • 2021-03-14
    • 2020-12-18
    • 2013-11-13
    • 2021-12-20
    • 2017-08-10
    • 2021-10-28
    相关资源
    最近更新 更多