#import "UIImageView+Video.h"
#import <SDWebImage/SDImageCache.h>
@implementation UIImageView (Video)
- (void)videoImageWithvideoURL:(NSURL *)videoURL atTime:(NSTimeInterval)time {
//先从缓存中查找是否有图片
SDImageCache *cache = [SDImageCache sharedImageCache];
UIImage *memoryImage = [cache imageFromMemoryCacheForKey:videoURL.absoluteString];
if (memoryImage) {
self.image = memoryImage;
return;
}else{
//再从磁盘中查找是否有图片
UIImage *diskImage = [cache imageFromDiskCacheForKey:videoURL.absoluteString];
if (diskImage) {
self.image = diskImage;
return;
}
}
if (!time) {
time = 1;
}
//如果都不存在,开启异步线程截取对应时间点的画面,转成图片缓存至磁盘
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
dispatch_async(dispatch_get_main_queue(), ^{
SDImageCache * cache = [SDImageCache sharedImageCache];
[cache storeImage:thumbnailImage forKey:videoURL.absoluteString toDisk:YES completion:nil];
self.image = thumbnailImage;
});
});
}
iOS 获取视频封面
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...