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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
//  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()<UICollectionViewDelegate, UICollectionViewDataSource, SDPhotoBrowserDelegate>
@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<detailsImageArr.count; i++) {
        NSDictionary *item = detailsImageArr[i];
        NSString *fileName = [item valueForKey:@"bf_filename"];
        BOOL isVideo = [fileName hasSuffix:@".mp4"];
        if (isVideo) {
            [videoArr addObject:item];
        }else {
            [imageArr addObject:item];
        }
    }
    if (videoArr.count > 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