1. 網(wǎng)絡(luò)URL
+ (UIImage*)getThumbnailImage:(NSString*)videoURL{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoURL] options:nil];
NSParameterAssert(asset);//斷言
AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
NSTimeInterval time = 0.1;
CGImageRefthumbnailImageRef =NULL;
CFTimeIntervalthumbnailImageTime = time;
NSError*error =nil;
thumbnailImageRef = [assetImageGeneratorcopyCGImageAtTime:CMTimeMake(thumbnailImageTime,60)actualTime:NULLerror:&error];
if( error ) {
NSLog(@"%@", error );
}
if( thumbnailImageRef ) {
return[[UIImagealloc]initWithCGImage:thumbnailImageRef];
}
return nil;
}
1. AVAssetImageGeneratorApertureModeCleanAperture
en:An image's clean aperture is a region of video free from transition artifacts caused by the encoding of the signal.
desc: 圖像的干凈光圈是一個沒有信號編碼引起的過渡偽影的視頻區(qū)域
2. AVAssetImageGeneratorApertureModeProductionAperture
en:The image is not cropped to the clean aperture region, but it is scaled according to the pixel aspect ratio. Use this option when you want to see all the pixels in your video, including the edges.
desc: 圖像不會被裁剪到干凈的光圈區(qū)域茁肠,但會根據(jù)像素長寬比進行縮放季惩。如果您想要查看視頻中的所有像素(包括邊緣)画髓,請使用此選項掘剪。
3. AVAssetImageGeneratorApertureModeEncodedPixels
en: The image is not cropped to the clean aperture region and is not scaled according to the pixel aspect ratio. The encoded dimensions of the image description are displayed.
desc: 圖像不會被裁剪到干凈的光圈區(qū)域,也不會根據(jù)像素長寬比進行縮放奈虾。顯示圖像說明的編碼尺寸夺谁。
有沒有大佬給解釋下上面三個的具體意思以及區(qū)別是什么,拜謝肉微!
其實主要就是AVAssetImageGenerator 這個類匾鸥,有其他需求研究這個類,非常有用碉纳,因為產(chǎn)品需求只是第一幀勿负,所以時間是0.1,可以根據(jù)自己需求設(shè)置
2. 本地URL
+ (UIImage *)xy_getVideoThumbnail:(NSString *)filePath
{
NSURL *sourceURL = [NSURL fileURLWithPath:filePath];
AVAsset *asset = [AVAsset assetWithURL:sourceURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMake(0, 1);
NSError *error;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
return thumbnail;
}