美文网首页iOS 成长之路
iOS 获取视频缩略图

iOS 获取视频缩略图

作者: 奋拓达 | 来源:发表于2018-06-02 13:27 被阅读5次

    iOS 获取视频缩略图

    首先导入AVFoundation库

    UIImagePickerController
    • 调用摄像头拍照,摄像的时候会用到UIImagePickerController
    // 实例化一个对象
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    // 设置图片的类型
    imagePicker.sourceType = [UIImagePickerControllerSourceTypeCamera];
    // 设置媒体的种类
    imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    // 允许编辑
    imagePicker.allowsEditing = NO;
    // 闪灯光模式
    imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    // 摄像的前置或者后置
    imagePicker.cameraDevice = UIImagePickerControllerCameraRear;
    imagePicker.delegate = self;
    // 通过模态视图推出视图页面
    [self presentViewController:imagePicker animated:YES completion:nil];
    

    注释:参数说明

    sourceType 的三种类型分别是

    • UIImagePickerControllerSourceTypeCamera 照相机
    • UIImagePickerControllerSourceTypePhotoLibrary 照片库 (用户不可删除)
    • UIImagePickerControllerSourceTypeSavedPhotosAlbum 通过摄像头保存的照片(用户可删除)
    实现PickerController的代理属性
    • 需要给它设置代理,然后遵守两个协议 ,实现下面的代理方法:
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    
    • 通常情况下,在didFinish方法里面对照片或视频进行保存处理;在didCancel方法里dismissViewController

    获取视频缩略图

    - (UIImage *)getScreenShotImageFromVideoPath:(NSString *)filePath{
    
        UIImage *shotImage;
        //视频路径URL
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
    
        AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
        gen.appliesPreferredTrackTransform = YES;
    
        CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    
        NSError *error = nil;
    
        CMTime actualTime;
    
        CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
        shotImage = [[UIImage alloc] initWithCGImage:image];
    
        CGImageRelease(image);
    
        return shotImage;
    
    }
    

    结语:这是一个简单实现获取视频缩略图的小案例。本文并没有涉及很深度的解读视频内容。本人在AVFoundation框架没有过多的研究。望原谅~

    相关文章

      网友评论

        本文标题:iOS 获取视频缩略图

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