我使用UIImagePickerController编辑视频并将其剪切, 我这样创建它:
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];
但不幸的是,在委托方法中,我无法获得开始和结束编辑时间 - 保存已编辑的视频:
-(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"];
}
在我的情况下,开始和结束是零。我不知道如何获取这些信息。你能告诉我如何获得它吗? 提前感谢您的帮助。
答案 0 :(得分:1)
我只是检查您的代码然后运行,我发现_UIImagePickerControllerVideoEditingStart
和_UIImagePickerControllerVideoEditingEnd
仅返回来自相机的已捕获视频,但如果您是从PhotoLibrary
中选择视频,那么您没有得到这两个键入信息词典:
如果您使用的是imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
,那么您将获得以下信息词典日志:
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";
如果您使用的是imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
,那么您将获得以下信息词典日志:
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";
所以最后UIImagePickerControllerSourceTypePhotoLibrary
UIImagePickerController不会提供_UIImagePickerControllerVideoEditingStart
和_UIImagePickerControllerVideoEditingEnd
键。因此,您可以使用AVURLAsset
来获取其持续时间,例如以下代码假设开始时间为0且结束时间为完整持续时间。
AVURLAsset *Asset = [AVURLAsset assetWithURL:videoURL];
CMTime duration = [Asset duration];
int fullduration = ceil(duration.value/duration.timescale);