Melany Melany - 5 months ago 60
iOS Question

Cant get start and end editing time in UIImagePickerController didFinishPickingMediaWithInfo:

I use UIImagePickerController to edit video and cut it,
I create it like this:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[imagePicker setVideoQuality:UIImagePickerControllerQualityType640x480];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = @[(__bridge NSString *)kUTTypeMovie, (__bridge NSString *)kUTTypeVideo, (__bridge NSString *)kUTTypeMPEG4];
imagePicker.allowsEditing = YES;
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];


but unfortunately in delegate method that is called I cant get start and end editing time - to save edited video:

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

[picker dismissViewControllerAnimated:YES completion:^{

}];

NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];


NSNumber *start = [info objectForKey:@"_UIImagePickerControllerVideoEditingStart"];
NSNumber *end = [info objectForKey:@"_UIImagePickerControllerVideoEditingEnd"];

}


in my case start and end is nil. I have no idea how to get this information. Could you please advice how to get it?
Thank you in advance for any help.

Answer

I just check your code and run and i found that _UIImagePickerControllerVideoEditingStart and _UIImagePickerControllerVideoEditingEnd return only captured video from camera but if you are select video from your PhotoLibrary you did not get this two key in info dictionary:

If you are using imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; then you will get following info dictionary log:

    UIImagePickerControllerMediaType = "public.movie";
    UIImagePickerControllerMediaURL = "file:///private/var/mobile/Containers/Data/Application/BCBE6A30-1A92-41BB-8734-BFF052E416DE/tmp/trim.59737F15-FC2A-4EB7-89AE-DF3831089657.MOV";
    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.MOV?id=87E3E50A-1DBF-449F-8BE3-AB36E718D82C&ext=MOV";

If you are using imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; then you will get following info dictionary log:

    UIImagePickerControllerMediaType = "public.movie";
    UIImagePickerControllerMediaURL = "file:///private/var/mobile/Containers/Data/Application/611D33A4-61F4-4CD0-934D-C0EFF82B75AC/tmp/capture/capturedvideo.MOV";
    "_UIImagePickerControllerVideoEditingEnd" = "2.360877976190476";
    "_UIImagePickerControllerVideoEditingStart" = "1.360505952380952";

So at the end with UIImagePickerControllerSourceTypePhotoLibrary UIImagePickerController wont provide a _UIImagePickerControllerVideoEditingStart and _UIImagePickerControllerVideoEditingEnd key. so you can get its duration by using AVURLAsset like following code that assume start Time is 0 and end time is full duration.

AVURLAsset *Asset = [AVURLAsset assetWithURL:videoURL];
CMTime duration = [Asset duration];
int fullduration = ceil(duration.value/duration.timescale);