ORStudios
ORStudios

Reputation: 3233

Crash When Selecting Video For UIImagePickerController

I have a UIImagePicker that is being used to pull in videos on my new application. I have noticed that according to Crashlytics I am receiving a number of crashes where the below movieURL is nil when inserting into the movie array.

I tested it before release with a number of videos and all seems to be fine. As mentioned however since release I have had a number of crashes.

The error report in question is:

Exception: NSInvalidArgumentException 
*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

The actual code the receives the video is as follows:

-(void)imagePickerController:( UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {

    NSURL *movieURL = [info valueForKey:@"UIImagePickerControllerMediaURL"];

    CompileViewController *compileViewController = [[CompileViewController alloc] init];
    compileViewController.movieArray = [[NSMutableArray alloc] init];
    [compileViewController.movieArray insertObject:movieURL atIndex:0];

     [picker dismissViewControllerAnimated:NO completion:nil];

    [self.navigationController pushViewController:compileViewController animated:NO];


}

Here is the code for selecting the video with the picker.

-(IBAction)loadVideo {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    //imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    [self presentViewController:imagePicker animated:NO completion:nil];

}

I was wondering what could be causing the user to provide a nil result for "UIImagePickerControllerMediaURL" and how I could go about resolving this issue. I have looked at trying to capture an error but I don't think the method has one. I think I could check for the nil value before entering it into the array but I would like to know why the result is coming back as nil in the first place.

Thanks

Upvotes: 3

Views: 1098

Answers (1)

Bill Patterson
Bill Patterson

Reputation: 2558

Are you properly limiting the media selection choices for your UIImagePickerController? It is possible, depending on configuration, to not get what you are expecting.

Your code is assuming that the value you are retrieving from the dictionary will exist and be valid. Your runtime crashes are telling you that's not true.

Could you post the setup code where you configure your UIImagePickerController? In particular, have you done things like verify availability (using isSourceTypeAvailable: and availableMediaTypesForSourceType:), etc?

If you're leaving open any possible path for user to get out of you UIImagePickerController without recording a movie, there will be no file and hence no URL pointing to it (url will be nil).

Edit --

Based on your comments, I don't see any obvious problems. But there may be issues with configuration, movie format and/or user-granted permissions for access. I'd act on these two warnings from documentation:

Always call the isSourceTypeAvailable: class method of the UIImagePickerController class and respect its return value. Never assume that a device has a photo library. Even if the device has a library, this method could still return NO if the library is currently unavailable.

and

[media type]: However, before setting this property, check which media types are available by calling the availableMediaTypesForSourceType: class method.

Upvotes: 2

Related Questions