//
|
// PBEAddImageView.m
|
// IphoneBIMe
|
//
|
// Created by zjf on 2020/12/15.
|
// Copyright © 2020 ProBIM. All rights reserved.
|
//
|
|
#import "PBEAddImageView.h"
|
#import "PBImageCollectionViewCell.h"
|
static NSString *const cellID = @"cellID";
|
@interface PBEAddImageView()<UICollectionViewDelegate, UICollectionViewDataSource, SDPhotoBrowserDelegate>
|
@property (nonatomic, strong) UICollectionView *collectionView;
|
@property (nonatomic, strong) UIButton *photoBtn;
|
@property (nonatomic, weak) UIButton *addPhotoBtn;
|
@end
|
@implementation PBEAddImageView
|
- (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(122, 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;
|
}
|
- (void)uploadPhoto {
|
if (self.ChooseImageBlock) {
|
self.ChooseImageBlock();
|
}
|
}
|
|
- (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) {
|
return self.imageArr.count;
|
}else {
|
return self.detailsImageArr.count;
|
}
|
}
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
|
cell.image = self.imageArr[indexPath.row];
|
[cell circleViewWithRadius:6];
|
if (self.isAddExamine) {
|
cell.roleType = CREATOR;//PARTICIPANT
|
cell.DeleteImageBlock = ^{
|
if (self.DeleteImageBlock) {
|
NSInteger index = indexPath.row;
|
self.DeleteImageBlock(index);
|
}
|
};
|
}else {
|
NSDictionary *RelationDoc = self.detailsImageArr[indexPath.row];
|
NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[RelationDoc valueForKey:@"bf_path"]];
|
cell.imageUrl = url;
|
cell.roleType = PARTICIPANT;
|
}
|
return cell;
|
}
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
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.addPhotoBtn.hidden = NO;
|
self.collectionView.hidden = NO;
|
[self.collectionView reloadData];
|
}else {
|
self.collectionView.hidden = YES;
|
self.addPhotoBtn.hidden = YES;
|
}
|
|
}
|
-(void)setDetailsImageArr:(NSArray *)detailsImageArr {
|
_detailsImageArr = detailsImageArr;
|
self.collectionView.hidden = NO;
|
self.addPhotoBtn.hidden = YES;
|
[self.collectionView reloadData];
|
}
|
- (void)setIsAddExamine:(BOOL)isAddExamine {
|
_isAddExamine = isAddExamine;
|
}
|
/*
|
// Only override drawRect: if you perform custom drawing.
|
// An empty implementation adversely affects performance during animation.
|
- (void)drawRect:(CGRect)rect {
|
// Drawing code
|
}
|
*/
|
|
@end
|