user2328703
user2328703

Reputation: 225

UIImagePickerControllerReferenceURL is missing

I'm trying to get some data about a video i chose in the UIImagePicker.

So when it gets into the UIImagePicker delegate method (below) i understand i need to use the UIImagePickerControllerReferenceURL key from the info dictionary.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];

    NSLog(@"%@",url);

    [library addAssetURL:[info objectForKey:UIImagePickerControllerReferenceURL] toAlbum:@"Compedia" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];
}

The problem is that the url is resulting with nil. I printed the description of info and it looks like this:

Printing description of info:
{
    UIImagePickerControllerMediaType = "public.movie";
    UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/tmp/capture-T0x1f57e880.tmp.Ulfn5o/capturedvideo.MOV";
}

After i've done some research i found out that if i set the camera with kUTTypeMovie it should give me both media and reference url.

This is how i defined my camera:

cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = YES;
cameraUI.delegate = delegate;
cameraUI.showsCameraControls = NO;
cameraUI.cameraOverlayView = [self getCustomToolBar];

Is there something i'm missing?

Thanks,

Upvotes: 6

Views: 3374

Answers (2)

Dimple Shah
Dimple Shah

Reputation: 303

ALAssetsLibrary is deprecated... use PHFetchOptions..

Refer below answer: https://stackoverflow.com/a/44328085/5315917

Upvotes: 0

ssh88
ssh88

Reputation: 403

Nearly went insane on this one. Every where I looked everyone seemed to be convinced that the UIImagePickerControllerReferenceURL was always there.

Anyway the issue is that if your delegate is being called after you have taken a pic using the camera than the UIImagePickerControllerReferenceURL object will not be there as the image has not been saved to the camera roll yet so therefore there is no UIImagePickerControllerReferenceURL. It is only there when the delegate is called after selecting an image from the camera roll.

So my way around this is to use the ALAssetLibrary to save it to the camera roll then read back the URL.

Heres my solution:

First you want to detect your PickerController sourceType to see if it was the camera or photosLibrary/savedPhotosAlbum. If it is the camera then we use the Asset library's writeImageToSavedPhotosAlbum:orientation:completionBlock: to write the image to the camera roll and give us back the images URL in the completion block.

However if the its not the camera then that means is was either the photosLibrary or savedPhotosAlbum which in both cases [info objectForKey:UIImagePickerControllerReferenceURL] would return a valid url

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

//dismiss the imagepicker view
[self dismissViewControllerAnimated:YES completion:nil];


if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    ALAssetsLibrary *library = [Utils defaultAssetsLibrary];
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
     {
         //here is your URL : assetURL
     }];
}
else
{
    //else this is valid : [info objectForKey:UIImagePickerControllerReferenceURL]];
}

}

Hope that helps.

Upvotes: 13

Related Questions