将图像上传到Firebase存储只会覆盖SwiftUI/Swift 5中的当前照片吗? [英] Uploading an image to firebase storage only overrides current photo in SwiftUI/Swift 5?

查看:44
本文介绍了将图像上传到Firebase存储只会覆盖SwiftUI/Swift 5中的当前照片吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现一个问题,当我将应用程序中的图像上传到Firebase存储时,它会覆盖存储中的当前图像.就像我上载新照片时一样,我想将新照片与已经在其中的旧照片一起显示在存储设备中.我们可以对下面的代码做些什么吗?谢谢你的帮助.

Coming to a problem where when I upload an image in my app to firebase storage it just overrides the current image I have on my storage. Like when I upload a new photo I want to show the new photo in my storage along with my old photo that is already in there. Can we do something with my code below? thanks for the help.

这是我的代码:

    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()
    }
}


 
 

推荐答案

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

When you upload an image to Firebase, you have to tell it the path where you want to save the image. In your code, you are putting the data to storage.reference().child("photo"). If you want to store multiple images, you need to change the "photo" path, otherwise it will overwrite the current image at the path. I would also recommend using sub-folders.

我会这样称呼:

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

这篇关于将图像上传到Firebase存储只会覆盖SwiftUI/Swift 5中的当前照片吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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