【问题标题】:Uploading an image to firebase storage only overrides current photo in SwiftUI/Swift 5?将图像上传到 Firebase 存储只会覆盖 SwiftUI/Swift 5 中的当前照片?
【发布时间】:2020-12-22 19:59:06
【问题描述】:

遇到一个问题,当我将应用中的图像上传到 Firebase 存储时,它只会覆盖我存储中的当前图像。就像当我上传一张新照片时,我想在我的存储空间中显示新照片以及已经存在的旧照片。我们可以用下面的代码做点什么吗?感谢您的帮助。

这是我的代码:

    struct UploadPhoto: View {
    @State var showActionSheet = false
    @State var showImagePicker = false
    
    @State var sourceType:UIImagePickerController.SourceType = .camera
    
    @State var upload_image:UIImage?
    @State var download_image:UIImage?
    
    @State private var showsAlert = false
     
    
    var body: some View {
       
        
      
        VStack {
            Text("Please upload your photos here!")
            HStack{
         
                if upload_image != nil {
                Image(uiImage: upload_image!)
                .resizable()
                .scaledToFit()
                    .frame(width:120, height:120, alignment: .center)
            } else {
                Image(systemName: "photo")
                .resizable()
                .scaledToFit()
                .frame(width:200, height:200, alignment: .center)
            }
 
            }.padding()
           
            Button(action: {
                self.showActionSheet = true
            }) {
                Text("Choose...")
            }.actionSheet(isPresented: $showActionSheet){
                ActionSheet(title: Text("Choose from camera or photo library..."), message: nil, buttons: [
                    //Button1
                    
                    .default(Text("Camera"), action: {
                        self.showImagePicker = true
                        self.sourceType = .camera
                    }),
                  
                    .default(Text("Photo Library"), action: {
                        self.showImagePicker = true
                        self.sourceType = .photoLibrary
                    }),
                    
                   
                    .cancel()
                    
                ])
            }.sheet(isPresented: $showImagePicker){
                imagePicker(image: self.$upload_image, showImagePicker: self.$showImagePicker, sourceType: self.sourceType)
                
            }
            
        
            Button(action: {
               
                if let thisImage = self.upload_image {
                    uploadImage(image: thisImage)
                 
                    
                } else {
                    
                    print("")
                }
                
                 
            }) {
              
                Text("Submit")
                 
                }
            }
 
        }
        
        
    }
    func uploadImage(image:UIImage){
        
        if let imageData = image.jpegData(compressionQuality: 1){
            
            let storage = Storage.storage()
            
            storage.reference().child("photo").putData(imageData, metadata: nil){
                (_, err) in
                if let err = err {
                 print("an error has occurred - \(err.localizedDescription)")
       }
                } else {
                     
                    print("Your photos have been successfully  
                }
            }
        } else {
            print("coldn't unwrap/case image to data")
        }
    }
 
 

struct UploadPhoto_Previews: PreviewProvider {
    static var previews: some View {
        UploadPhoto()
    }
}


 
 

【问题讨论】:

    标签: swift firebase swiftui firebase-storage


    【解决方案1】:

    当您将图片上传到 Firebase 时,您必须告诉它您要保存图片的路径。在您的代码中,您将数据放入 storage.reference().child("photo")。如果要存储多张图片,则需要更改“照片”路径,否则会覆盖该路径处的当前图片。我还建议使用子文件夹。

    我会这样称呼:

    let imageName = "image1"
    let folderName = "folder1"
    let path = "\(folderName)/\(imageName)"
    storage.reference(withPath: path).putData...
    

    【讨论】:

    • 我实现了你的 sn-p。但是,它会创建文件夹,然后在其中添加 image1,然后当我上传另一个文件夹时,它会覆盖它。
    • 每次调用函数时都需要更改路径(imageName和/或folderName)。
    • 我们可以不用我一直改变路径而换一种方式吗?
    • 不,哈哈,这确实是为了您的利益,因为当您从 Firebase 下载图像时,您还需要指定下载路径。我会在视图上创建另一个变量并将该变量传递到路径中。通常的做法是使用某种 ID 作为文件夹名称,然后是照片的描述符。类似“(userID)/(profilepicture)”
    • 没问题.. 如果您遇到问题,请告诉我。如果有效,请标记为正确答案:)
    猜你喜欢
    • 2020-07-10
    • 2020-12-25
    • 2019-04-18
    • 2016-12-27
    • 2020-05-20
    • 1970-01-01
    • 2018-12-01
    • 2019-08-08
    • 2017-06-16
    相关资源
    最近更新 更多