通视频URL截取第一帧图片

来源:互联网 发布:工程预算套定额软件 编辑:程序博客网 时间:2024/05/16 19:56

为了方便直接给UIImage加个类别,以后什么时候使用可以直接调用。

#import <UIKit/UIKit.h>

 

@interface UIImage (Video)

 

/**

 通过视频URL获取视频的第一帧图片

 

 @param videoURL 视频连接

 @return 第一帧图片

 */

+ (UIImage *)interceptFirstTimeVideoPicture:(NSURL *)videoURL;

 

@end

 

-----------------------------------分割线------------------------

#import "UIImage+Video.h"

#import <AVFoundation/AVFoundation.h>

 

@implementation UIImage (Video)

 

 

+ (UIImage *)interceptFirstTimeVideoPicture:(NSURL *)videoURL{

    

    // 根据视频的URL创建AVURLAsset

    AVURLAsset *asset = [[AVURLAsset allocinitWithURL:videoURL options:nil];

   

    // 根据AVURLAsset创建AVAssetImageGenerator对象

     AVAssetImageGenerator* gen = [[AVAssetImageGenerator allocinitWithAsset: asset];

    

     gen.appliesPreferredTrackTransform = YES;

    

    // 定义获取0帧处的视频截图

    CMTime time = CMTimeMake(010);

    

    NSError *error = nil;

    

    CMTime actualTime;

    

    // 获取time处的视频截图

    CGImageRef  image = [gen  copyCGImageAtTime: time actualTime: &actualTime error:&error];

    

    // CGImageRef转换为UIImage

    UIImage *thumb = [[UIImage allocinitWithCGImage: image];

    

    CGImageRelease(image);

    

    return  thumb;

    

}

 

 

@end

 

注意点:

使用AVAssetImageGenerator这个类是要记得导入#import <AVFoundation/AVFoundation.h>头文件。


阅读全文
1 0
原创粉丝点击