6

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,

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
user2328703
  • 227
  • 2
  • 11

2 Answers2

13

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.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129
ssh88
  • 393
  • 1
  • 3
  • 14
  • I am getting an error at this statement: ALAssetsLibrary *library = [Utils defaultAssetsLibrary]; (Use of undeclared identifier Utils) What am I missing here? – Azeem Shaikh Jul 21 '14 at 19:28
  • @AzeemShaikh Sorry that was my fault, i have a utils calls that creates a singleton of ALAssetsLibrary as is best practice to only have one of these in your app. There are a few articles,stack posts and Apple documentation online explaining this. So you just need to replace [Utils defaultAssetsLibrary] with a new instance of ALAssetsLibrary. However like i said its best that you have a singleton of this in your app. – ssh88 Aug 11 '14 at 10:16
0

ALAssetsLibrary is deprecated... use PHFetchOptions..

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

Dimple Shah
  • 304
  • 3
  • 18