IphoneBIMe/IphoneBIMe/Classes/Project/Issue/Views/AddIssueView/PBVideoCollectionViewCell.m
@@ -10,6 +10,7 @@
@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 {
@@ -49,7 +50,37 @@
}
- (void)setVideoUrl:(NSString *)videoUrl {
    _videoUrl = videoUrl;
    NSString *imageStr = [videoUrl stringByReplacingOccurrencesOfString:@".mp4" withString:@"_image.png"];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@""]];
//    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