Lion Lion - 1 month ago 4
iOS Question

UIImagePickerController - picking video in background from photo library

How to continue pick video from photo library in background mode ?

I mean when I press

use
button from
imagePickerController - PhotoLibrary
and video is started to get compressing - During this compression process (have attached screenshot) if I press
home button(i.e. go to background)
and then come to
foreground
then I got
info[UIImagePickerControllerMediaURL]
as
null
, so is it possible that app can continue compressing video in background also and return proper
url
when come to
foreground
?

Screenshot :

enter image description here

My
didFinishPickingMediaWithInfo
,

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


NSURL *url = info[UIImagePickerControllerMediaURL];

NSLog(@"url : %@",url);

[picker dismissViewControllerAnimated:YES completion:nil];

}


P.S : If we record video by
camera
and go to background then it will stop recording there and we can use it after coming foreground.

I have thought of one workaround -
UIBackgroundTaskIdentifier
but it will not work in every case, if video is big then it have time limit, so looking for any other solution!

Any help will be appreciated! :)

Answer

IF we want to pick video continuously in background by UIImagePickerController from photolibrary during video is compressing and user press home button then we have to use UIBackgroundTaskIdentifier for continue execution in background or any other background way which keeps app working in backgroun(less possible thing!). Now, UIBackgroundTaskIdentifier has time limit so we can't pick any size of video, so if we restricted video time duration then we can pick it continuously in background something like,

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.videoMaximumDuration = 60.0;

 self.backgroundTask = UIBackgroundTaskInvalid;

            self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                NSLog(@"Background handler called. Not running background tasks anymore.");
                [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
                self.backgroundTask = UIBackgroundTaskInvalid;
            }];

[self presentViewController:picker animated:YES completion:NULL]; 

With UIImagePickerController we can do that much only to pick videon in background. If anyone want to pick large video in background then he/she should take look into ALAssetLibrary or Photos framework.