iOS7编程Cookbook中例15.8中一个小问题

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;)


该书的15.8样例标题为Editing Videos on an iOS Device,代码的功能为创建一个UIImagePickerController视图让用户从照片库选择一个视频文件,然后在UIVideoEditorController视图中编辑该视频,最后得到编辑后视频文件的路径.

非常好非常easy,可是在实际执行代码中发现当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];
        }
    }
posted on 2017-07-31 09:15  blfbuaa  阅读(170)  评论(0编辑  收藏  举报