我这边没有去缓存加载,直接从本地沙盒取,防止sd缓存过高
//
// UIImageView+GYVideoImage.m
// GuiYuSiri
//
// Created by wbb on 2023/2/27.
//
#import "UIImageView+GYVideoImage.h"
#import <SDWebImage/SDImageCache.h>
@implementation UIImageView (GYVideoImage)
- (void)gy_videoImageWithVideoURL:(NSURL *)videoURL {
[self gy_videoImageWithVideoURL:videoURL atTime:1];
}
- (void)gy_videoImageWithVideoURL:(NSURL *)videoURL atTime:(NSTimeInterval)time {
//本地查找是否有图片
SDImageCache *cache = [SDImageCache sharedImageCache];
NSString *filepath= [cache cachePathForKey:videoURL.absoluteString];
UIImage *memoryImage = [UIImage imageWithContentsOfFile:filepath];
if (memoryImage) {
[self setImage:memoryImage];
return;
}
if (!time) {
time = 1;
}
//如果都不存在,开启异步线程截取对应时间点的画面,转成图片缓存至磁盘
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetGen.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
assetGen.appliesPreferredTrackTransform = YES;
CFTimeInterval thumbnailImageTime = time;
CMTime time = CMTimeMake(thumbnailImageTime, 60);//CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
CGFloat width = ViewWidth - 32 - 12;
CGSize imageSize = thumbnailImage.size;
CGFloat height = imageSize.height/imageSize.width*1.f * width;
// 图片压缩
UIImage *videoImage = [GlobalFunc thumbnailWithImageWithoutScale:thumbnailImage size:CGSizeMake(width, height)];
dispatch_async(dispatch_get_main_queue(), ^{
SDImageCache * cache = [SDImageCache sharedImageCache];
[cache storeImage:videoImage forKey:videoURL.absoluteString toDisk:YES completion:nil];
[self setImage:videoImage];
});
});
}
@end
下面这种情况属于三级缓存,先从缓存查找,再从本地磁盘中查找,然后获取视频桢图片
//
// UIImageView+GYVideoImage.m
// GuiYuSiri
//
// Created by wbb on 2023/2/27.
//
#import "UIImageView+GYVideoImage.h"
#import <SDWebImage/SDImageCache.h>
@implementation UIImageView (GYVideoImage)
- (void)gy_videoImageWithVideoURL:(NSURL *)videoURL {
[self gy_videoImageWithVideoURL:videoURL atTime:1];
}
- (void)gy_videoImageWithVideoURL:(NSURL *)videoURL atTime:(NSTimeInterval)time {
//先从缓存中查找是否有图片
SDImageCache *cache = [SDImageCache sharedImageCache];
UIImage *memoryImage = [cache imageFromMemoryCacheForKey:videoURL.absoluteString];
if (memoryImage) {
[self setImage:memoryImage];
return;
}else{
//再从磁盘中查找是否有图片
UIImage *diskImage = [cache imageFromDiskCacheForKey:videoURL.absoluteString];
if (diskImage) {
[self setImage: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;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetGen.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
assetGen.appliesPreferredTrackTransform = YES;
CFTimeInterval thumbnailImageTime = time;
CMTime time = CMTimeMake(thumbnailImageTime, 60);//CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
dispatch_async(dispatch_get_main_queue(), ^{
SDImageCache * cache = [SDImageCache sharedImageCache];
[cache storeImage:videoImage forKey:videoURL.absoluteString toDisk:YES completion:nil];
[self setImage:videoImage];
});
});
}
@end
网友评论