iOS 获取视频缩略图

根据视频资源的路径获取视频的缩略图
+(UIImage *)getThumbnailImage:(NSString *)videoPath {    
    if (videoPath) {
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath: videoPath] options:nil];        
        AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];        
        // 设定缩略图的方向
        // 如果不设定,可能会在视频旋转90/180/270°时,获取到的缩略图是被旋转过的,而不是正向的
        gen.appliesPreferredTrackTransform = YES;    
        // 设置图片的最大size(分辨率)
        gen.maximumSize = CGSizeMake(300, 169);  
        CMTime time = CMTimeMakeWithSeconds(5.0, 600); //取第5秒,一秒钟600帧
        NSError *error = nil; 
        CMTime actualTime;
        CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];   
        if (error) {
            UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
            return placeHoldImg;
        }    
        UIImage *thumb = [[UIImage alloc] initWithCGImage:image];  
        CGImageRelease(image); 
        return thumb;     
    } else { 
         UIImage *placeHoldImg = [UIImage imageNamed:@"posters_default_horizontal"];
        return placeHoldImg;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值