gpt4 book ai didi

ios - 上传到 Firebase 时元数据出现异常 - ios - Objective c

转载 作者:行者123 更新时间:2023-11-28 23:54:24 28 4
gpt4 key购买 nike

我正在尝试从 iOS 应用程序将图像上传到 Firebase,我使用的是 objective-c 。现在,当我尝试初始化上传任务时,它抛出了一个异常:

2018-07-26 10:36:28.472418-0700 Geographical_Photo_Map[27942:1919716] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105a8d12b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000104bd4f41 objc_exception_throw + 48
2 CoreFoundation 0x0000000105acd0cc _CFThrowFormattedException + 194
3 CoreFoundation 0x00000001059a1951 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 321
4 CoreFoundation 0x00000001059a17db +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
5 Geographical_Photo_Map 0x0000000102609537 -[FIRStorageUploadTask enqueue] + 1415
6 Geographical_Photo_Map 0x00000001026038a8 -[FIRStorageReference putFile:metadata:completion:] + 1192
7 Geographical_Photo_Map 0x00000001026033c4 -[FIRStorageReference putFile:metadata:] + 116
8 Geographical_Photo_Map 0x000000010248e618 -[ViewController imagePickerController:didFinishPickingMediaWithInfo:] + 536
9 UIKit 0x0000000106c9ddef -[UIImagePickerController _imagePickerDidCompleteWithInfo:] + 127
10 UIKit 0x0000000106c9d709 __60-[UIImagePickerController didSelectMediaWithInfoDictionary:]_block_invoke + 42
11 libdispatch.dylib 0x0000000108b022f7 _dispatch_call_block_and_release + 12
12 libdispatch.dylib 0x0000000108b0333d _dispatch_client_callout + 8
13 libdispatch.dylib 0x0000000108b0e5f9 _dispatch_main_queue_callback_4CF + 628
14 CoreFoundation 0x0000000105a4fe39 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
15 CoreFoundation 0x0000000105a14462 __CFRunLoopRun + 2402
16 CoreFoundation 0x0000000105a13889 CFRunLoopRunSpecific + 409
17 GraphicsServices 0x000000010b2339c6 GSEventRunModal + 62
18 UIKit 0x00000001068425d6 UIApplicationMain + 159
19 Geographical_Photo_Map 0x000000010248f83f main + 111
20 libdyld.dylib 0x0000000108b7fd81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

好像是元数据问题,我只是加了

metadata.contentType = @"image/jpeg";

对于metadata,网上找的其他教程也是这样初始化的。我可以知道我应该如何处理这个问题吗?非常感谢您的帮助!我的代码:

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

// Get the selected image's NSURL.
// write to file and upload.
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];

// 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
}];
}

最佳答案

这是因为元数据类型不正确。

您可以通过替换行来修复:

NSData *imageData = UIImagePNGRepresentation(image);

与:

NSData *imageData = UIImageJPEGRepresentation(image);

或者你可以使用

metadata.contentType = @"image/png"

这取决于您真正想要哪种图像格式,PNG 或 JPEG

关于ios - 上传到 Firebase 时元数据出现异常 - ios - Objective c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51545101/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com