Skylar
Skylar

Reputation: 127

Exception with metadata when uploading to Firebase - ios - objective c

I'm trying to upload an image to Firebase from an ios application, I'm using objective c. Now when I try to initialize the upload task it throws an exception:

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

Seems like the problem was caused by metadata, I only added

metadata.contentType = @"image/jpeg";

To metadata, and other tutorials I found online initialize it in this way as well. May I know what should I do with this problem? Greatly appreciate your help! My code:

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

Upvotes: 0

Views: 236

Answers (1)

Victor Kwok
Victor Kwok

Reputation: 652

It's because the meta data type is not correct.

You can fix by replacing the line :

NSData *imageData = UIImagePNGRepresentation(image);

with:

NSData *imageData = UIImageJPEGRepresentation(image);

or you can use

metadata.contentType = @"image/png"

It depends on which image format you really want, PNG or JPEG

Upvotes: 1

Related Questions