如何使用iOS Swift在Firebase存储中上传图像阵列

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

我想按用户上传图像数组这是我将单个图像上传到Firebase存储时的代码,请参见代码:1.但是我在上传图像和数组时遇到问题。参考代码:2代码:1

func uploadImage(image:UIImage,userId:String,completion:@escaping(_ status:Bool,_ response:String)->Void){
        // if status is true then downloadurl will be in response
        // Data in memory
        guard let data = image.jpegData(compressionQuality: 0.2) else{
            completion(false,"Unable to get data from image")
            return
        }
        // Create a reference to the file you want to upload
        let riversRef = firebaseStorage.reference().child("images/\(userId).jpg")
        // Upload the file to the path "images/rivers.jpg"
        let _ = riversRef.putData(data, metadata: nil) { (metadata, error) in
            guard let _ = metadata else{
                // Uh-oh, an error occurred!
                completion(false,error!.localizedDescription)
                return
            }
            // You can also access to download URL after upload.
            riversRef.downloadURL { (url, error) in
                guard let downloadURL = url else{
                    // Uh-oh, an error occurred!
                    completion(false,error!.localizedDescription)
                    return
                }
                completion(true,downloadURL.absoluteString)
            }
        }
    }

代码:2

func uploadPostImages(image:[UIImage],userId:String,completion:@escaping(_ status:Bool,_ response:String)->Void){
        let photoDictionary = ["postImages": PostArray.sharedInstance.photosArray as NSArray]
         // Create a reference to the file you want to upload
         let riversRef = firebaseStorage.reference().child("postImages/\(userId).jpg")
         // Upload the file to the path "images/rivers.jpg"
         let _ = riversRef.putData(photoDictionary, metadata: nil) { (metadata, error) in
             guard let _ = metadata else{
                 // Uh-oh, an error occurred!
                 completion(false,error!.localizedDescription)
                 return
             }
             // You can also access to download URL after upload.
             riversRef.downloadURL { (url, error) in
                 guard let downloadURL = url else{
                     // Uh-oh, an error occurred!
                     completion(false,error!.localizedDescription)
                     return
                 }
                 completion(true,downloadURL.absoluteString)
             }
         }
     }

我在代码中收到此错误:2

Cannot convert value of type '[String : NSArray]' to expected argument type 'Data'

任何可以帮助我的人?​​

ios swift firebase firebase-storage
1个回答
0
投票

据我所知,类似的东西应该起作用。抱歉,我不在IDE中。如果这不起作用,请发表评论,我会仔细看。

func uploadImages(
images:[UIImage],
userId:String,
completion:@escaping(_ status:Bool,_ response:String)->Void)
{
        images.enumerated().forEach { (index, image) in 
        guard let data = image.jpegData(compressionQuality: 0.2) else{
            completion(false,"Unable to get data from image")
            return
        }
        // Create a reference to the file you want to upload
        let riversRef = firebaseStorage.reference().child("images/\(userId)\(index).jpg")
        // Upload the file to the path "images/rivers.jpg"
        let _ = riversRef.putData(data, metadata: nil) { (metadata, error) in
            guard let _ = metadata else{
                // Uh-oh, an error occurred!
                completion(false,error!.localizedDescription)
                return
            }
            // You can also access to download URL after upload.
            riversRef.downloadURL { (url, error) in
                guard let downloadURL = url else{
                    // Uh-oh, an error occurred!
                    completion(false,error!.localizedDescription)
                    return
                }
                completion(true,downloadURL.absoluteString)
            }
         }
       }
    }
© www.soinside.com 2019 - 2024. All rights reserved.