// // PBIssueHeaderView.m // IphoneBIMe // // Created by zjf on 2019/1/10. // Copyright © 2019 ProBIM. All rights reserved. // #import "PBIssueHeaderView.h" #import "PBImageCollectionViewCell.h" #import "PBProjectModel.h" static NSString *const ImageCellID = @"ImageCellID"; @interface PBIssueHeaderView () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, assign) BOOL isAddIssue; @property (nonatomic, strong) UIButton *selectBtn; @end @implementation PBIssueHeaderView - (instancetype)initWithFrame:(CGRect)frame { if (self == [super initWithFrame:frame]) { [self setupUI]; } return self; } - (void)setupUI { self.backgroundColor = [UIColor whiteColor]; UILabel *titleL = [UILabel z_labelWithText:@"关联图片(选填)" Color:PromptColor isBold:NO Font:TitleFontSize]; [self addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(13); make.left.equalTo(self).offset(16); make.height.equalTo(@22); make.width.equalTo(@132); }]; self.selectBtn = [UIButton z_textButton:@"请选择" fontSize:MarkedFontSize normalColor:TitleColor]; [self.selectBtn addTarget:self action:@selector(chooseImageAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.selectBtn]; [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self).offset(-16); make.centerY.equalTo(titleL); }]; UIView *lineV = [[UIView alloc] init]; lineV.backgroundColor = PBColor(240, 240, 240); [self addSubview:lineV]; [lineV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(16); make.left.equalTo(self).offset(20); make.right.equalTo(self).offset(-20); make.height.equalTo(@1); }]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(80, 80); layout.minimumLineSpacing = 10; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [self.collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:ImageCellID]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.bounces = NO; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.backgroundColor = [UIColor whiteColor]; [self addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lineV.mas_bottom).offset(16); make.left.equalTo(self).offset(33); make.right.equalTo(self).offset(-32); make.height.equalTo(@80); }]; self.collectionView.hidden = YES; UIView *bottomV = [[UIView alloc] init]; bottomV.backgroundColor = PBColor(242, 242, 242); [self addSubview:bottomV]; [bottomV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self); make.height.equalTo(@10); }]; } - (void)chooseImageAction:(UIButton *)button { if (self.ChooseImageBlock) { self.ChooseImageBlock(); } } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (self.isAddIssue) { if (self.viewPointImage != nil) { return self.imageArr.count + 1; }else { return self.imageArr.count; } }else { if (self.viewPointImageUrl != nil && ![self.viewPointImageUrl isEqualToString:@""]) { return self.imageDocArr.count + 1; }else { return self.imageDocArr.count; } } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellID forIndexPath:indexPath]; if (self.isAddIssue) { if (indexPath.row == 0 && self.viewPointImage != nil) { cell.image = self.viewPointImage; cell.viewPointFlag = @"viewPoint"; }else { NSInteger index = indexPath.row; if (self.viewPointImage) { index = indexPath.row - 1; } cell.image = self.imageArr[index]; } }else { if (indexPath.row == 0 && self.viewPointImageUrl != nil && ![self.viewPointImageUrl isEqualToString:@""]) { cell.imageUrl = self.viewPointImageUrl; cell.viewPointFlag = @"viewPoint"; }else { NSInteger index = indexPath.row; if (self.viewPointImageUrl != nil && ![self.viewPointImageUrl isEqualToString:@""]) { index = indexPath.row - 1; } NSDictionary *RelationDoc = self.imageDocArr[index]; // NSString *url = [NSString stringWithFormat:@"%@/api/Doc/GetHideFile?ProjectID=%@&FileId=%@&FileType=Issue",BimUrl,self.projectMdoel.bimcomposerid,[RelationDoc valueForKey:@"TargetID"]]; NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[RelationDoc valueForKey:@"bf_path"]]; cell.imageUrl = url; cell.roleType = self.roleType; } } cell.DeleteImageBlock = ^{ if (self.DeleteImageBlock) { NSInteger index = indexPath.row; if (self.isAddIssue) { if (self.viewPointImage) { index = indexPath.row - 1; } }else { if (self.viewPointImageUrl) { index = indexPath.row - 1; } } self.DeleteImageBlock(index); } }; cell.OpenViewPointBlock = ^{ if (self.OpenViewPointBlock) { self.OpenViewPointBlock(); } }; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new]; photoBrowser.delegate = self; photoBrowser.currentImageIndex = indexPath.item; if (self.isAddIssue) { if (self.viewPointImage) { photoBrowser.imageCount = self.imageArr.count + 1; }else { photoBrowser.imageCount = self.imageArr.count; } }else { if (self.viewPointImageUrl) { photoBrowser.imageCount = self.imageDocArr.count + 1; }else { photoBrowser.imageCount = self.imageDocArr.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.isAddIssue) { 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; self.isAddIssue = YES; [self.collectionView reloadData]; if (imageArr.count == 0 && self.viewPointImage == nil) { self.collectionView.hidden = YES; }else { self.collectionView.hidden = NO; } } - (void)setImageDocArr:(NSMutableArray *)imageDocArr { _imageDocArr = imageDocArr; self.isAddIssue = NO; [self.collectionView reloadData]; if (imageDocArr.count == 0 && [self.viewPointImageUrl isEqualToString:@""]) { self.collectionView.hidden = YES; }else { self.collectionView.hidden = NO; } } - (void)setProjectMdoel:(PBProjectModel *)projectMdoel { _projectMdoel = projectMdoel; } - (void)setRoleType:(RoleType)roleType { _roleType = roleType; if (roleType == CREATOR) { [self.selectBtn setTitleColor:TitleColor forState:UIControlStateNormal]; }else { [self.selectBtn setTitleColor:PromptColor forState:UIControlStateNormal]; } } - (void)setViewPointImage:(UIImage *)viewPointImage { _viewPointImage = viewPointImage; } - (void)setViewPointImageUrl:(NSString *)viewPointImageUrl { _viewPointImageUrl = viewPointImageUrl; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end