ios - 将图像上传到 firebase 时出现 NSInvalidArgumentException - ios

标签 ios objective-c firebase

我是 ios 开发的新手。我正在尝试按照Firebase的官方文档来实现图片上传功能。但是我遇到了以下错误:

错误日志如下:

2018-07-21 17:42:33.306219-0700 Geographical_Photo_Map[330:34159] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(0x18448fd38 0x1839a4528 0x184428c44 0x1843614b0 0x18436132c 0x100648b60 0x100642a34 0x100642570 0x1004cbcf8 0x18de02c68 0x18de025bc 0x100cb149c 0x100cb145c 0x100cb6050 0x184437f20 0x184435afc 0x1843562d8 0x1861e7f84 0x18d903880 0x1004cd024 0x183e7a56c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

调试器显示问题出在 FIRStorageUploadTask.m 中的这一行 NSDictionary *queryParams = @{@"uploadType": @"resumable", @"name": self.uploadMetadata.path};

我的代码在这里:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];

    // Get the selected image's NSURL.

    NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
    NSString *imageName = [imagePath lastPathComponent];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePathString = [documentsDirectory stringByAppendingPathComponent:imageName];
    NSURL *localFile = [NSURL URLWithString:localFilePathString];

    // Create the file metadata
    FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
    metadata.contentType = @"image/jpeg";

    // Get a reference to the storage service using the default Firebase App
    FIRStorage *storage = [FIRStorage storage];

    // Create a storage reference from our storage service
    FIRStorageReference *storageRef = [storage reference];

    // Upload file and metadata to the object 'images/mountains.jpg'
    FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile metadata:metadata];

    // Listen for state changes, errors, and completion of the upload.
    [uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
        // Upload resumed, also fires when the upload starts
    }];
}

- (IBAction)UploadButtonClicked:(id)sender {
    // Choose from photo library.
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

如果有人提供任何建议或解决方案,我将不胜感激!

最佳答案

这不是获取图像 URL 的正确方法。您应该先将图像保存在您的文档目录中。

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePathString = [documentsDirectory stringByAppendingPathComponent:@"yourImage.png"];

NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:localFilePathString atomically:YES];

NSURL *localFile = [NSURL fileURLWithPath:localFilePathString];

引用资料:

How to get URL image in UIImagepickercontroller

Swift 3.0 Getting URL of UIImage selected from UIImagePickerController

关于ios - 将图像上传到 firebase 时出现 NSInvalidArgumentException - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461157/

相关文章:

firebase - 什么时候可以在 Firebase 中嵌套?

javascript - 保存 FCM 数据库消息的时间戳

iphone - 当我在 iphone sdk 中发推文时应用程序崩溃

ios - 以编程方式快速更改 UIButton 的文本

java - 相当于 Objective-C 的 Guava Preconditions.checkNotNull(...) 吗?

ios - 大小与字体 : ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7

objective-c - 将 zxing 添加到 XCode 4

NodeJS 中的 Firebase 与 HTTP 代理

ios - Swift - 使用 selectedIndex 更改 var

ios - 如何围绕 Y 轴翻转 CALayer?