// // PBVideoCollectionViewCell.m // IphoneBIMe // // Created by zjf on 2020/8/25. // Copyright © 2020 ProBIM. All rights reserved. // #import "PBVideoCollectionViewCell.h" @interface PBVideoCollectionViewCell() @property (nonatomic, weak) UIImageView *imageV; @property (nonatomic, weak) UIButton *playBtn; typedef void(^MyImageBlock)(UIImage * _Nullable image); @end @implementation PBVideoCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { UIImageView *imageV = [[UIImageView alloc] init]; [self.contentView addSubview:imageV]; [imageV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.right.equalTo(self.contentView); }]; // UIViewContentModeScaleToFill, // UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent // UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped. // UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay) // UIViewContentModeCenter, imageV.contentMode = UIViewContentModeScaleAspectFill; imageV.backgroundColor = [UIColor blackColor]; UIButton *playBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"details_play"]]; [playBtn addTarget:self action:@selector(playViedo) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:playBtn]; [playBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.contentView); make.size.mas_equalTo(CGSizeMake(30, 30)); }]; self.imageV = imageV; self.playBtn = playBtn; } - (void)playViedo { if (self.PlayVideoBlock) { self.PlayVideoBlock(); } } - (void)setVideoUrl:(NSString *)videoUrl { _videoUrl = videoUrl; // NSString *imageStr = [videoUrl stringByReplacingOccurrencesOfString:@".mp4" withString:@"_image.png"]; // [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@""]]; [self getThumbnailImage:[NSURL URLWithString:videoUrl] completion:^(UIImage * _Nullable image) { self.imageV.image = image; }]; } - (void)getThumbnailImage:(NSURL *)videoURL completion:(MyImageBlock)handler { dispatch_async(dispatch_get_global_queue(0, 0), ^{ AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; generator.appliesPreferredTrackTransform = YES; CMTime time = CMTimeMakeWithSeconds(0.0, 600); NSError *error = nil; CMTime actualTime; CGImageRef imageRef = [generator copyCGImageAtTime:time actualTime:&actualTime error:&error]; UIImage *thumb = [[UIImage alloc] initWithCGImage:imageRef]; CGImageRelease(imageRef); dispatch_async(dispatch_get_main_queue(), ^{ handler(thumb); }); }); } @end