Skylar
Skylar

Reputation: 127

NSInvalidArgumentException when uploading images to firebase - ios

I'm a newbie to ios development. I'm trying to follow the official documentation of Firebase to implement an image uploading function. But I encountered the following error:

The error log is as follows:

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) 

The debugger shows the problem is in this line in FIRStorageUploadTask.m NSDictionary *queryParams = @{@"uploadType" : @"resumable", @"name" : self.uploadMetadata.path};

My code is here:

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

I will be greatly appreciated if someone offers any suggestions or solutions!

Upvotes: 0

Views: 292

Answers (1)

Victor Kwok
Victor Kwok

Reputation: 652

This is not the correct way to get the URL of the image. You should save the image in your documents directory first.

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

References:

How to get URL image in UIImagepickercontroller

Swift 3.0 Getting URL of UIImage selected from UIImagePickerController

Upvotes: 1

Related Questions