Lion Lion - 1 year ago 68
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

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
then I got
, so is it possible that app can continue compressing video in background also and return proper
when come to

Screenshot :

enter image description here


-(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
and go to background then it will stop recording there and we can use it after coming foreground.

I have thought of one workaround -
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download