// // PBCheckHeaderView.m // IphoneBIMe // // Created by zjf on 2020/12/15. // Copyright © 2020 ProBIM. All rights reserved. // #import "PBCheckHeaderView.h" #import "PBImageCollectionViewCell.h" static NSString *const cellID = @"cellID"; @interface PBCheckHeaderView() @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UIButton *photoBtn; @property (nonatomic, weak) UIButton *addPhotoBtn; @property (nonatomic, weak) UIView *videoBgV; @property (nonatomic, weak) UIImageView *videoImageV; @property (nonatomic, weak) UILabel *videoL; @property (nonatomic, weak) UIButton *deleteVideoBtn; @property (nonatomic, weak) UIButton *playVideoBtn; @property (nonatomic, copy) NSString *playUrl; typedef void(^MyImageBlock)(UIImage * _Nullable image); @end @implementation PBCheckHeaderView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { self.backgroundColor = PBColor(244, 245, 246); UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.width - 20, 90)]; bgV.backgroundColor = [UIColor whiteColor]; [self addSubview:bgV]; [bgV circleViewWithRadius:6]; self.photoBtn = [[UIButton alloc] init]; [self.photoBtn setImage:[UIImage imageNamed:@"add_phone"] forState:UIControlStateNormal]; [self.photoBtn setTitle:@"添加图片或视频" forState:UIControlStateNormal]; [self.photoBtn setTitleColor:PBColor(97, 111, 125) forState:UIControlStateNormal]; self.photoBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [self.photoBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 98)]; [self.photoBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [self.photoBtn addTarget:self action:@selector(uploadPhoto) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.photoBtn]; [self.photoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(43); make.centerX.equalTo(self); make.size.mas_equalTo(CGSizeMake(142, 24)); }]; [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.equalTo(bgV).offset(10); make.bottom.right.equalTo(bgV).offset(-10); }]; self.collectionView.hidden = YES; UIButton *addPhotoBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"image_camera"]]; [addPhotoBtn addTarget:self action:@selector(uploadPhoto) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:addPhotoBtn]; [addPhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.right.bottom.equalTo(bgV); make.width.equalTo(@90); }]; self.addPhotoBtn = addPhotoBtn; self.addPhotoBtn.hidden = YES; UIView *videoBgV = [[UIView alloc] init]; videoBgV.backgroundColor = [UIColor whiteColor]; [self addSubview:videoBgV]; [videoBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.right.equalTo(self.collectionView); }]; UIImageView *videoImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 70, 70)]; [self addSubview:videoImageV]; [videoImageV circleViewWithRadius:6]; UILabel *videoL = [UILabel z_labelWithText:@"可添加一条60s内视频或者6张图片添加图片请删除视频" Color:PBColor(97, 111, 125) isBold:NO Font:13]; videoL.numberOfLines = 0; [self addSubview:videoL]; [videoL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(videoBgV); make.left.equalTo(videoImageV.mas_right).offset(15); make.width.equalTo(@210); }]; UIButton *deleteVideoBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"video_account_delete"]]; [self addSubview:deleteVideoBtn]; [deleteVideoBtn addTarget:self action:@selector(deleteVideo) forControlEvents:UIControlEventTouchUpInside]; [deleteVideoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(videoBgV).offset(-10); make.bottom.equalTo(videoBgV); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; UIButton *playVideoBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"small_play"]]; [self addSubview:playVideoBtn]; [playVideoBtn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside]; [playVideoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(videoImageV); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; self.videoBgV = videoBgV; self.videoImageV = videoImageV; self.playVideoBtn = playVideoBtn; self.videoL = videoL; self.deleteVideoBtn = deleteVideoBtn; self.videoBgV.hidden = YES; self.videoImageV.hidden = YES; self.playVideoBtn.hidden = YES; self.videoL.hidden = YES; self.deleteVideoBtn.hidden = YES; } - (void)uploadPhoto { if (self.ChooseImageBlock) { self.ChooseImageBlock(); } } - (void)playVideo { NSLog(@"开始播放"); if (self.isAddExamine) { if (self.PlayVideoBlock) { self.PlayVideoBlock(); } }else { if (self.PlayVideoUrlBlock) { self.PlayVideoUrlBlock(self.playUrl); } } } - (void)deleteVideo { NSLog(@"删除视频"); if (self.DeleteVideoBlock) { self.DeleteVideoBlock(); } } - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(70, 70); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [_collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:cellID]; _collectionView.delegate = self; _collectionView.dataSource = self; // _collectionView.pagingEnabled = YES; // _collectionView.bounces = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 100); } return _collectionView; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (self.isAddExamine) { if (self.imageArr.count >= 4) { self.addPhotoBtn.hidden = NO; return self.imageArr.count; }else { self.addPhotoBtn.hidden = YES; return self.imageArr.count + 1; } }else { return self.detailsImageArr.count; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; if (self.isAddExamine) { if (indexPath.row == self.imageArr.count) { cell.image = [UIImage imageNamed:@"img_camera_2"]; cell.roleType = PARTICIPANT; }else { cell.image = self.imageArr[indexPath.row]; cell.roleType = CREATOR; } cell.DeleteImageBlock = ^{ if (self.DeleteImageBlock) { self.DeleteImageBlock(indexPath.row); } }; }else { NSDictionary *RelationDoc = self.detailsImageArr[indexPath.row]; NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[RelationDoc valueForKey:@"bf_path"]]; cell.imageUrl = url; cell.roleType = PARTICIPANT; } [cell circleViewWithRadius:6]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.imageArr.count < 4 & indexPath.row == self.imageArr.count & self.isAddExamine) { if (self.ChooseImageBlock) { self.ChooseImageBlock(); } }else { SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new]; photoBrowser.delegate = self; photoBrowser.currentImageIndex = indexPath.item; if (self.isAddExamine) { photoBrowser.imageCount = self.imageArr.count; }else { photoBrowser.imageCount = self.detailsImageArr.count; } photoBrowser.sourceImagesContainerView = self.collectionView; [photoBrowser show]; } } #pragma mark SDPhotoBrowserDelegate // 返回临时占位图片(即原来的小图) - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { // 不建议用此种方式获取小图,这里只是为了简单实现展示而已 PBImageCollectionViewCell *cell = (PBImageCollectionViewCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; if (self.isAddExamine) { return cell.imageV.image; }else { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:cell.imageUrl]]; UIImage *image = [UIImage imageWithData:data];; return image; } } - (void)setImageArr:(NSMutableArray *)imageArr { _imageArr = imageArr; if (_imageArr.count > 0){ self.collectionView.hidden = NO; [self.collectionView reloadData]; }else { self.collectionView.hidden = YES; self.addPhotoBtn.hidden = YES; } self.videoBgV.hidden = YES; self.videoImageV.hidden = YES; self.videoL.hidden = YES; self.deleteVideoBtn.hidden = YES; self.playVideoBtn.hidden = YES; } - (void)setVideoArr:(NSMutableArray *)videoArr { _videoArr = videoArr; if (videoArr.count > 0) { self.collectionView.hidden = YES; self.addPhotoBtn.hidden = YES; self.videoBgV.hidden = NO; self.videoImageV.hidden = NO; self.videoL.hidden = NO; self.deleteVideoBtn.hidden = NO; self.playVideoBtn.hidden = NO; PBResultVideo *result = videoArr[0]; self.videoImageV.image = result.coverImage; }else { self.collectionView.hidden = YES; self.addPhotoBtn.hidden = YES; self.videoBgV.hidden = YES; self.videoImageV.hidden = YES; self.videoL.hidden = YES; self.deleteVideoBtn.hidden = YES; self.playVideoBtn.hidden = YES; } } - (void)setDetailsImageArr:(NSArray *)detailsImageArr { _detailsImageArr = detailsImageArr; NSMutableArray *imageArr = [[NSMutableArray alloc] init]; NSMutableArray *videoArr = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i 0) { self.collectionView.hidden = YES; self.addPhotoBtn.hidden = YES; self.videoBgV.hidden = NO; self.videoImageV.hidden = NO; self.videoL.hidden = YES; self.deleteVideoBtn.hidden = YES; self.playVideoBtn.hidden = NO; self.playUrl = [NSString stringWithFormat:@"%@%@",BaseUrl,[videoArr[0] valueForKey:@"bf_path"]]; [self getThumbnailImage:[NSURL URLWithString:self.playUrl] completion:^(UIImage * _Nullable image) { self.videoImageV.image = image; }]; }else { self.collectionView.hidden = NO; [self.collectionView reloadData]; self.videoBgV.hidden = YES; self.videoImageV.hidden = YES; self.videoL.hidden = YES; self.deleteVideoBtn.hidden = YES; self.playVideoBtn.hidden = YES; } } - (void)setIsAddExamine:(BOOL)isAddExamine { _isAddExamine = isAddExamine; } - (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); }); }); } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end