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;
    }
}

你可能感兴趣的:(iOS 获取视频缩略图)