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

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

当我尝试将图像上传到 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()
    }
}
ios swift swiftui firebase-storage
2个回答
1
投票

发现问题出在我这边。

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

我做了与上面类似的事情。 确保使用孩子的参考资料。您不能在根文件夹中写入内容。

所以

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

0
投票

我也有类似的问题。我的 firebase 上传失败,但错误没有显示。相反,它会因该错误而崩溃。 React Native 在异步钩子方面遇到了困难,它会吸收错误而不是显示错误,不仅在 useEffect 中,而且在常规钩子中也是如此。在网络上,您可以在钩子中运行异步函数,但显然不能使用 React Native。一旦我将异步函数转换为 .then 函数,我就能看到错误。这是违反规则的行为。

© www.soinside.com 2019 - 2024. All rights reserved.