ios 当上传图像到Firebase存储时,尝试从objects[1]插入nil对象

rt4zxlrg  于 2023-03-31  发布在  iOS
关注(0)|答案(1)|浏览(170)

我得到这个错误,当我试图上传一张图片到firebase存储.我试图重写当前视图,并改变一些事情,但再次当我点击上传按钮的应用程序崩溃,我得到了相同的错误日志.我所知道的是,错误是在函数中,我调用时,用户上传照片到firebase存储.
注:我是iOS开发的初学者:-)
谢谢。

错误:

libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
terminating with uncaught exception of type NSException

我的密码:

struct SelectProfileImageView: 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?
    
    
    var body: some View {
        VStack {
            HStack{
            //Uploading image
                if upload_image != nil {
                Image(uiImage: upload_image!)
                .resizable()
                .scaledToFit()
                    .frame(width:120, height:120)
            } else {
                Image(systemName: "timelapse")
                .resizable()
                .scaledToFit()
                    .frame(width:300, height:300)
            }
                Spacer()
                if download_image != nil {
                    Image(uiImage: download_image!)
                    .resizable()
                    .scaledToFit()
                        .frame(width:120, height:120)
                } else {
                    Image(systemName: "timelapse")
                    .resizable()
                    .scaledToFit()
                        .frame(width:120, height:120)
                }
            }.padding()
            
            Button(action: {
                self.showActionSheet = true
            }, label: {
                Text("Select Image")
            }).actionSheet(isPresented: $showActionSheet){
                ActionSheet(title: Text("Add a picture to your post"), message: nil, buttons: [
                    //Button1
                    
                    .default(Text("Camera"), action: {
                        self.showImagePicker = true
                        self.sourceType = .camera
                    }),
                    //Button2
                    .default(Text("Photo Library"), action: {
                        self.showImagePicker = true
                        self.sourceType = .photoLibrary
                    }),
                    
                    //Button3
                    .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("Upload Image")
            }
            
            
        }
    }
}

func uploadImage(image:UIImage){
    if let imageData = image.jpegData(compressionQuality: 1){
        let storage = Storage.storage()
        storage.reference().putData(imageData, metadata: nil) {
            (_, err) in
            if let err = err {
                print("an error has occurred - \(err.localizedDescription)")
            } else {
                print("image uploaded successfully")
            }
        }
    } else {
        print("coldn't unwrap/case image to data")
    }
}

struct SelectProfileImageView_Previews: PreviewProvider {
    static var previews: some View {
        SelectProfileImageView()
    }
}
kcwpcxri

kcwpcxri1#

发现了我这边的问题所在。

let storage = Storage.storage()
storage.reference().putData(imageData, metadata: nil) {

我做了和上面类似的事情,一定要用孩子的引用,不能在根文件夹里写东西。
所以

let storage = Storage.storage()
storage.reference().child("some/path").putData(imageData, metadata: nil) {

相关问题