DKPhotoPickerCollectionView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // PickerCollectionView.m
  3. // 相机
  4. //
  5. //
  6. #import "DKPhotoPickerCollectionView.h"
  7. #import "DKPhotoPickerCollectionViewCell.h"
  8. #import "DKPhotoPickerImageView.h"
  9. #import "DKPhotoPickerFooterCollectionReusableView.h"
  10. #import <AssetsLibrary/AssetsLibrary.h>
  11. #import "DKPhotoAssets.h"
  12. #import "DkPhoto.h"
  13. @interface DKPhotoPickerCollectionView () <UICollectionViewDataSource,UICollectionViewDelegate>
  14. @property (nonatomic , strong) DKPhotoPickerFooterCollectionReusableView *footerView;
  15. // 判断是否是第一次加载
  16. @property (nonatomic , assign , getter=isFirstLoadding) BOOL firstLoadding;
  17. @end
  18. @implementation DKPhotoPickerCollectionView
  19. #pragma mark -getter
  20. - (NSMutableArray *)selectsIndexPath{
  21. if (!_selectsIndexPath) {
  22. _selectsIndexPath = [NSMutableArray array];
  23. }
  24. return _selectsIndexPath;
  25. }
  26. #pragma mark -setter
  27. - (void)setDataArray:(NSArray *)dataArray{
  28. _dataArray = dataArray;
  29. // 需要记录选中的值的数据
  30. if (self.isRecoderSelectPicker){
  31. NSMutableArray *selectAssets = [NSMutableArray array];
  32. for (DKPhotoAssets *asset in self.selectAsstes) {
  33. for (DKPhotoAssets *asset2 in self.dataArray) {
  34. if ([asset.asset.defaultRepresentation.url isEqual:asset2.asset.defaultRepresentation.url]) {
  35. [selectAssets addObject:asset2];
  36. break;
  37. }
  38. }
  39. }
  40. _selectAsstes = selectAssets;
  41. }
  42. [self reloadData];
  43. }
  44. - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
  45. if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
  46. self.backgroundColor = [UIColor clearColor];
  47. self.dataSource = self;
  48. self.delegate = self;
  49. _selectAsstes = [NSMutableArray array];
  50. }
  51. return self;
  52. }
  53. #pragma mark -<UICollectionViewDataSource>
  54. - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  55. return 1;
  56. }
  57. - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  58. return self.dataArray.count;
  59. }
  60. - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  61. DKPhotoPickerCollectionViewCell *cell = [DKPhotoPickerCollectionViewCell cellWithCollectionView:collectionView cellForItemAtIndexPath:indexPath];
  62. DKPhotoPickerImageView *cellImgView = [[DKPhotoPickerImageView alloc] initWithFrame:cell.bounds];
  63. cellImgView.maskViewFlag = YES;
  64. // 需要记录选中的值的数据
  65. if (self.isRecoderSelectPicker) {
  66. for (DKPhotoAssets *photoasset in self.selectAsstes) {
  67. ALAsset* alasset=photoasset.asset;
  68. DKPhotoAssets* dkalasset=self.dataArray[indexPath.item];
  69. if ([alasset.defaultRepresentation.url isEqual:[dkalasset asset].defaultRepresentation.url]) {
  70. [self.selectsIndexPath addObject:@(indexPath.row)];
  71. }
  72. }
  73. }
  74. [cell.contentView addSubview:cellImgView];
  75. cellImgView.maskViewFlag = ([self.selectsIndexPath containsObject:@(indexPath.row)]);
  76. DKPhotoAssets *asset = self.dataArray[indexPath.item];
  77. cellImgView.isVideoType = asset.isVideoType;
  78. if ([asset isKindOfClass:[DKPhotoAssets class]]) {
  79. cellImgView.image = asset.thumbImage;
  80. }
  81. return cell;
  82. }
  83. #pragma mark - <UICollectionViewDelegate>
  84. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  85. if (!self.lastDataArray) {
  86. self.lastDataArray = [NSMutableArray array];
  87. }
  88. DKPhotoPickerCollectionViewCell *cell = (DKPhotoPickerCollectionViewCell *) [collectionView cellForItemAtIndexPath:indexPath];
  89. DKPhotoAssets *asset = self.dataArray[indexPath.row];
  90. DKPhotoPickerImageView *pickerImageView = [cell.contentView.subviews lastObject];
  91. // 如果没有就添加到数组里面,存在就移除
  92. if (pickerImageView.isMaskViewFlag) {
  93. [self.selectsIndexPath removeObject:@(indexPath.row)];
  94. [self.selectAsstes removeObject:asset];
  95. [self.lastDataArray removeObject:asset];
  96. }else{
  97. // 1 判断图片数超过最大数或者小于0
  98. NSUInteger minCount = (self.minCount < 0) ? KPhotoShowMaxCount : self.minCount;
  99. if (self.selectAsstes.count >= minCount-self.cameraCount) {
  100. NSString *format = [NSString stringWithFormat:@"最多只能选择%zd张图片",minCount];
  101. if (minCount == 0) {
  102. format = [NSString stringWithFormat:@"您已经选满了图片呦."];
  103. }
  104. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:format delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil];
  105. [alertView show];
  106. return ;
  107. }
  108. [self.selectsIndexPath addObject:@(indexPath.row)];
  109. [self.selectAsstes addObject:asset];
  110. [self.lastDataArray addObject:asset];
  111. }
  112. // 告诉代理现在被点击了!
  113. if ([self.collectionViewDelegate respondsToSelector:@selector(pickerCollectionViewDidSelected: deleteAsset:)]) {
  114. if (pickerImageView.isMaskViewFlag) {
  115. // 删除的情况下
  116. [self.collectionViewDelegate pickerCollectionViewDidSelected:self deleteAsset:asset];
  117. }else{
  118. [self.collectionViewDelegate pickerCollectionViewDidSelected:self deleteAsset:nil];
  119. }
  120. }
  121. pickerImageView.maskViewFlag = ([pickerImageView isKindOfClass:[DKPhotoPickerImageView class]]) && !pickerImageView.isMaskViewFlag;
  122. }
  123. #pragma mark 底部View
  124. -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  125. {
  126. DKPhotoPickerFooterCollectionReusableView *reusableView = nil;
  127. if (kind == UICollectionElementKindSectionFooter) {
  128. DKPhotoPickerFooterCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
  129. footerView.count = self.dataArray.count;
  130. reusableView = footerView;
  131. self.footerView = footerView;
  132. }else{
  133. }
  134. return reusableView;
  135. }
  136. - (void)layoutSubviews{
  137. [super layoutSubviews];
  138. // 时间置顶的话
  139. if (self.status == DKPickerCollectionViewShowOrderStatusTimeDesc) {
  140. if (!self.firstLoadding && self.contentSize.height > [[UIScreen mainScreen] bounds].size.height) {
  141. // 滚动到最底部(最新的)
  142. [self scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.dataArray.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
  143. // 展示图片数
  144. self.contentOffset = CGPointMake(self.contentOffset.x, self.contentOffset.y + 100);
  145. self.firstLoadding = YES;
  146. }
  147. }
  148. }
  149. - (void)dealloc{
  150. [[NSNotificationCenter defaultCenter] removeObserver:self];
  151. }
  152. @end