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
//
//  PBCommentsFooterView.m
//  IphoneBIMe
//
//  Created by zjf on 2018/9/4.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBCommentsFooterView.h"
#import "PBImageCollectionViewCell.h"
 
#define ALineCount 3
 
#define Spacing 10
static NSString *const cellID = @"cellID";
@interface PBCommentsFooterView()<UICollectionViewDataSource, UICollectionViewDelegate, SDPhotoBrowserDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat itemWH;
@end
@implementation PBCommentsFooterView
 
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(16);
        make.left.equalTo(self).offset(Spacing);
        make.right.equalTo(self).offset(-Spacing);
        make.bottom.equalTo(self).offset(-16);
    }];
}
 
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init];
        CGFloat itemWH = (PBScreenWidth - (ALineCount + 1) * Spacing) / ALineCount;
        self.itemWH = itemWH;
        layout.itemSize = CGSizeMake(itemWH, itemWH);
        layout.minimumLineSpacing = Spacing;
        layout.minimumInteritemSpacing = Spacing;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
    }
    return _collectionView;
}
 
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectedPhotos.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.image = self.selectedPhotos[indexPath.row];
    cell.roleType = CREATOR;
    cell.DeleteImageBlock = ^{
        if (self.deleteImageBlock) {
            self.deleteImageBlock(indexPath.row);
        }
    };
    return cell;
}
 
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = indexPath.item;
    photoBrowser.imageCount = self.selectedPhotos.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]];
    return cell.imageV.image;
}
- (void)setSelectedPhotos:(NSMutableArray *)selectedPhotos {
    _selectedPhotos = selectedPhotos;
    [self.collectionView reloadData];
}
 
@end