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;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容