zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
//  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