我是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];
}
如果有人提供任何建议或解决方案,我将不胜感激!
答案 0 :(得分:1)
这不是获取图像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