// // YMPersonalDynamicAlbumCell.m // MSYOUPAI // // Created by YoMi on 2024/3/3. // Copyright © 2024 MS. All rights reserved. // #import "YMPersonalDynamicAlbumCell.h" #import "YMPersonalDynamicAlbumCellViewModel.h" #import "YBIBVideoData.h" #import @interface YMPersonalDynamicAlbumCell() /// ViewModel @property (nonatomic, strong) YMPersonalDynamicAlbumCellViewModel *viewModel; /// 基础视图 @property (nonatomic, strong) UIView *baseView; /// 相册图片视图 @property (nonatomic, strong) UIImageView *albumImageView; @end @implementation YMPersonalDynamicAlbumCell - (void)ym_setupViews{ self.contentView.backgroundColor = UIColor.clearColor; self.backgroundColor = UIColor.clearColor; [self.contentView addSubview:self.baseView]; [self.baseView addSubview:self.albumImageView]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView); make.left.equalTo(self.contentView); make.right.equalTo(self.contentView); make.bottom.equalTo(self.contentView); }]; [self.albumImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.baseView); make.left.equalTo(self.baseView); make.right.equalTo(self.baseView); make.bottom.equalTo(self.baseView); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMPersonalDynamicAlbumCellViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; if([viewModel.albumType isEqualToString:@"mp4"]){ NSURL *url = [LCTools getImageUrlWithAddress:viewModel.albumUrl]; if(url){ [self videoImageWithvideoURL:url completion:^(UIImage *image) { // 主线程执行: dispatch_async(dispatch_get_main_queue(), ^{ [self.albumImageView setImage:image]; }); }]; } }else{ [self.albumImageView sd_setImageWithURL:[[LCTools getImageUrlWithAddress:self.viewModel.albumUrl] urlWithImageScale:50]]; } } - (UIView *)baseView{ if (!_baseView) { _baseView = [[UIView alloc]init]; } return _baseView; } - (UIImageView *)albumImageView{ if (!_albumImageView) { _albumImageView = [[UIImageView alloc]init]; _albumImageView.contentMode = UIViewContentModeScaleAspectFill; _albumImageView.clipsToBounds = YES; _albumImageView.layer.cornerRadius = adapt(10); _albumImageView.layer.masksToBounds = YES; } return _albumImageView; } //获取视频预览图,这里用到了SDImage的缓存图片功能: - (void)videoImageWithvideoURL:(NSURL *)videoURL completion:(void (^_Nullable)(UIImage *image))completionBlock{ //先从缓存中找是否有图片 SDImageCache *cache = [SDImageCache sharedImageCache]; UIImage *memoryImage = [cache imageFromMemoryCacheForKey:videoURL.absoluteString]; if(memoryImage) { completionBlock(memoryImage); return; }else{ UIImage*diskImage = [cache imageFromDiskCacheForKey:videoURL.absoluteString]; if(diskImage) { completionBlock(diskImage); return; } } 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.requestedTimeToleranceAfter = kCMTimeZero;//必须设置,否则时间对应不上 assetImageGenerator.requestedTimeToleranceBefore = kCMTimeZero;//必须设置,否则时间对应不上 assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels; CGImageRef thumbnailImageRef = NULL; CFTimeInterval thumbnailImageTime = 1; NSError *thumbnailImageGenerationError =nil; thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(thumbnailImageTime, 10) actualTime:NULL error:&thumbnailImageGenerationError]; if(!thumbnailImageRef) NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError); UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage:thumbnailImageRef] :nil; if(thumbnailImage){ completionBlock(thumbnailImage); dispatch_async(dispatch_get_main_queue(), ^{ SDImageCache *cache = [SDImageCache sharedImageCache]; // [cache storeImageToMemory:thumbnailImage forKey:videoURL.absoluteString]; [cache storeImage:thumbnailImage forKey:videoURL.absoluteString completion:^{ }]; }); } }); } @end