1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
//  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