美文网首页iOS技术
TZImagePickerController使用记录 PHAs

TZImagePickerController使用记录 PHAs

作者: 帅气的阿斌 | 来源:发表于2018-05-18 13:21 被阅读866次

    坑点1:选择完视频后 setDidFinishPickingVideoHandle block回调拿取到了PHAsset *phasset对象,

    如何将PHAsset将转成NSURL呢?实际上我们在使用TZImagePickerController选择视频时,TZImagePickerController提供视频预览功能,由此可以推断出这个TZImagePickerController应该是实现了获取资源路径的方法,于是找了下TZVideoPlayerController就是预览播放器,里面有个方法[TZImageManager manager] getVideoWithAsset...

    以下是最后获取到NSURL的代码:(demo看情况 有空再上传吧)

    要导入的头文件

    #import <TZImagePickerController.h>

    #import <TZImageManager.h>

    选择完视频后的回调

                        [tzImagePickerController setDidFinishPickingVideoHandle:^(UIImage *coverImage, PHAsset *phasset) {

                            [[TZImageManager manager] getVideoWithAsset:phasset completion:^(AVPlayerItem *playerItem, NSDictionary *info) {

                                AVAsset *avasset = playerItem.asset;

                                NSURL *fileUrl = [LZBRecordVideoTool _convert2Mp4:nil asset:avasset];//AVAsset转MP4并返回NSURL的实现

                                dispatch_async(dispatch_get_main_queue(), ^{

                                        //主线程中进行UI操作

                                });

                        }];

    AVAsset转MP4并返回NSURL的实现

    /*! @method @brief mov格式视频转换为MP4格式 @discussion @param movUrl   mov视频路径 @result  MP4格式视频路径 */

    + (NSURL *)_convert2Mp4:(NSURL *)movUrl asset:(AVAsset *)asset;

    {

        AVAsset *avAsset = asset;

        NSURL *mp4Url = nil;

        if (asset){

        }else{

            avAsset = [AVURLAsset URLAssetWithURL:movUrl options:nil];

        }

        NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

        if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {

            AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality];

            NSString *mp4Path = [NSString stringWithFormat:@"%@/%d%d.mp4", [EMCDDeviceManager dataPath], (int)[[NSDate date] timeIntervalSince1970], arc4random() % 100000];

            mp4Url = [NSURL fileURLWithPath:mp4Path];

            exportSession.outputURL = mp4Url;

            exportSession.shouldOptimizeForNetworkUse = YES;

            exportSession.outputFileType = AVFileTypeMPEG4;

            dispatch_semaphore_t wait = dispatch_semaphore_create(0l);

            [exportSession exportAsynchronouslyWithCompletionHandler:^{

                switch ([exportSession status]) {

                    case AVAssetExportSessionStatusFailed: {

                        NSLog(@"failed, error:%@.", exportSession.error);

                    } break;

                    case AVAssetExportSessionStatusCancelled: {

                        NSLog(@"cancelled.");

                    } break;

                    case AVAssetExportSessionStatusCompleted: {

                        NSLog(@"completed.");

                    } break;

                    default: {

                        NSLog(@"others.");

                    } break;

                }

                dispatch_semaphore_signal(wait);

            }];

            long timeout = dispatch_semaphore_wait(wait, DISPATCH_TIME_FOREVER);

            if (timeout) {

                NSLog(@"timeout.");

            }

            if (wait) {

                //dispatch_release(wait);

                wait = nil;

            }

        }

        return mp4Url;

    }

    相关文章

      网友评论

        本文标题:TZImagePickerController使用记录 PHAs

        本文链接:https://www.haomeiwen.com/subject/enkgdftx.html