大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)
该书的15.8例子标题为Editing Videos on an iOS Device,代码的功能为创建一个UIImagePickerController视图让用户从照片库选择一个视频文件,然后在UIVideoEditorController视图中编辑该视频,最后得到编辑后视频文件的路径.
很好很简单,但是在实际运行代码中发现当UIImagePickerController返回后,在imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary*)info回调方法中获取不到被选择视频的URL路径:
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) {
_videoURLToEdit = info[UIImagePickerControllerMediaURL];
}
即执行后的_videoURLToEdit总为nil,即使前面的资源类型是正确的:为kUTTypeMovie类型.
网上搜了一下,发现很多人说在iOS9里面都是这样,包括Stack OF里面也是如此.但不怎么相信此说法…
后来到Apple SDK中查找info字典的解释:
info
A dictionary containing the original image and the edited image, if an image was picked; or a filesystem URL for the movie, if a movie was picked. The dictionary also contains any relevant editing information. The keys for this dictionary are listed in Editing Information Keys.
可以看到其所有key的解释:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
NSString *const UIImagePickerControllerLivePhoto;
注意看其中包含一个UIImagePickerControllerReferenceURL枚举,其说明称该key的值为原始资源的URL.而UIImagePickerControllerMediaURL里的值是当原始资源被修改后的URL.
我们前面总是返回nil,是因为我们没有修改原始资源,所以总为空值.
我们简单的将代码修改如下即可:
if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]) {
_videoURLToEdit = info[UIImagePickerControllerMediaURL];
if (!_videoURLToEdit) {
_videoURLToEdit = info[UIImagePickerControllerReferenceURL];
}
}