Collection_Image.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // Collection_Image.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/1/9.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "Collection_Image.h"
  9. #import "CollectionImageCell.h"//cell
  10. #import "ImageDetailBigView.h"//detail
  11. @interface Collection_Image()<ImagePickerVCDelegate>
  12. @property (nonatomic, strong) XG_AssetModel *placeholderModel;
  13. @end
  14. @implementation Collection_Image
  15. #pragma mark 懒加载
  16. -(NSMutableArray<XG_AssetModel *> *)assets{
  17. if (!_assets) {
  18. _assets = @[self.placeholderModel].mutableCopy;
  19. }
  20. return _assets;
  21. }
  22. -(XG_AssetModel *)placeholderModel{
  23. if (!_placeholderModel) {
  24. _placeholderModel = [[XG_AssetModel alloc]init];
  25. _placeholderModel.isPlaceholder = YES;
  26. }
  27. return _placeholderModel;
  28. }
  29. - (NSMutableArray *)aryPhoto{
  30. if (!_aryPhoto) {
  31. _aryPhoto = [NSMutableArray new];
  32. }
  33. return _aryPhoto;
  34. }
  35. - (void)openAlbum{
  36. WEAKSELF
  37. [[XG_AssetPickerManager manager] handleAuthorizationWithCompletion:^(XG_AuthorizationStatus aStatus) {
  38. if (aStatus == XG_AuthorizationStatusAuthorized) {
  39. [weakSelf showAssetPickerController];
  40. }else{
  41. [weakSelf showAlert];
  42. }
  43. }];
  44. }
  45. - (void)showAssetPickerController{
  46. XG_AssetPickerOptions *options = [[XG_AssetPickerOptions alloc]init];
  47. options.maxAssetsCount = 9;
  48. options.videoPickable = false;
  49. NSMutableArray<XG_AssetModel *> *array = self.assets;
  50. [array removeLastObject];//去除占位model
  51. options.pickedAssetModels = array;
  52. XG_AssetPickerController *photoPickerVc = [[XG_AssetPickerController alloc] initWithOptions:options delegate:self];
  53. [GB_Nav pushViewController:photoPickerVc animated:true];
  54. }
  55. - (void)showAlert{
  56. ModelBtn * modelDismiss = [ModelBtn modelWithTitle:@"取消" imageName:nil highImageName:nil tag:TAG_LINE color:[UIColor redColor]];
  57. ModelBtn * modelConfirm = [ModelBtn modelWithTitle:@"去设置" imageName:nil highImageName:nil tag:TAG_LINE color:COLOR_BLUE];
  58. modelConfirm.blockClick = ^(void){
  59. //去设置
  60. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  61. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  62. [[UIApplication sharedApplication] openURL:url];
  63. }
  64. };
  65. [BaseAlertView initWithTitle:@"" content:@"未开启相册权限,是否去设置中开启?" aryBtnModels:@[modelDismiss,modelConfirm] viewShow:[UIApplication sharedApplication].keyWindow];
  66. }
  67. - (void)onDeleteBtnClick:(UIButton *)sender{
  68. /*
  69. performBatchUpdates并不会调用代理方法collectionView: cellForItemAtIndexPath,
  70. 如果用删除按钮的tag来标识则tag不会更新,所以此处没有用tag
  71. */
  72. CollectionImageCell *cell = (CollectionImageCell *)sender.superview.superview;
  73. NSIndexPath *indexpath = [self.collectionView indexPathForCell:cell];
  74. [self.collectionView performBatchUpdates:^{
  75. [self.collectionView deleteItemsAtIndexPaths:@[indexpath]];
  76. [self.assets removeObjectAtIndex:indexpath.item];
  77. NSMutableArray *arr = [NSMutableArray array];
  78. for (XG_AssetModel *model in self.assets) {
  79. [arr addObject:@(model.isPlaceholder)];
  80. }
  81. if (self.assets.count == 8 && (![self.assets containsObject:self.placeholderModel]&&![arr containsObject:@(YES)])) {
  82. [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:8 inSection:0]]];
  83. [self.assets addObject:self.placeholderModel];
  84. }
  85. } completion:^(BOOL finished) {
  86. }];
  87. NSMutableArray *newAssets = self.assets.mutableCopy;
  88. NSMutableArray *arr = [NSMutableArray array];
  89. for (int i =0; i<self.assets.count; i++) {
  90. XG_AssetModel *model =self.assets[i];
  91. model.number = 0;
  92. for (int j=0; j<newAssets.count; j++) {
  93. XG_AssetModel *pam = newAssets[j];
  94. if ([model.asset.localIdentifier isEqualToString:pam.asset.localIdentifier]) {
  95. model.picked = YES;
  96. model.number = j+1;
  97. [arr addObject:model];
  98. }
  99. }
  100. }
  101. if (self.blockDelComplete) {
  102. self.blockDelComplete(self.assets);
  103. }
  104. }
  105. #pragma mark - UICollectionViewDataSource
  106. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  107. return self.assets.count;
  108. }
  109. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  110. CollectionImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CollectionImageCell class]) forIndexPath:indexPath];
  111. cell.imgName=self.imgName;
  112. cell.model = self.assets[indexPath.item];
  113. // [cell resetWithModel:self.assets[indexPath.item]];
  114. [cell.btnDelete addTarget:self action:@selector(onDeleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  115. WEAKSELF
  116. [self.aryPhoto removeAllObjects];
  117. cell.blockModel = ^(UIImage *photo) {
  118. [weakSelf.aryPhoto addObject:photo];
  119. };
  120. return cell;
  121. }
  122. #pragma mark - UICollectionViewDelegate
  123. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  124. if (!isAry(self.assets)) {
  125. [self.assets addObject:self.placeholderModel];
  126. }
  127. XG_AssetModel *model = self.assets[indexPath.item];
  128. if (model.isPlaceholder) {
  129. [self openAlbum];
  130. }
  131. }
  132. #pragma mark - XG_AssetPickerControllerDelegate
  133. - (void)assetPickerController:(XG_AssetPickerController *)picker didFinishPickingAssets:(NSArray<XG_AssetModel *> *)assets{
  134. NSMutableArray *newAssets = assets.mutableCopy;
  135. if (newAssets.count < 9 ) {
  136. [newAssets addObject:self.placeholderModel];
  137. }
  138. [self.assets removeAllObjects];
  139. [self.assets addObjectsFromArray: newAssets];
  140. NSMutableArray *arr = [NSMutableArray arrayWithCapacity:9];
  141. for (int j = 0; j < 9; j++) {
  142. [arr addObject:[NSNull null]];
  143. }
  144. NSMutableArray *arru = [NSMutableArray array];
  145. dispatch_queue_t serialQueue=dispatch_queue_create("串行", DISPATCH_QUEUE_SERIAL);
  146. dispatch_sync(serialQueue, ^{
  147. for (int i =0; i<assets.count; i++) {
  148. XG_AssetModel *model =assets[i];
  149. if (model.url) {
  150. [arru addObject:model];
  151. }
  152. if (model.asset) {
  153. [[XG_AssetPickerManager manager] getPhotoWithAsset1:model.asset completion:^(UIImage *photo, NSDictionary *info) {
  154. if (isAry(arru)) {
  155. [arr replaceObjectAtIndex:model.number-arru.count-1 withObject:photo];
  156. }else{
  157. [arr replaceObjectAtIndex:model.number-1 withObject:photo];
  158. }
  159. }];
  160. }
  161. }
  162. });
  163. if (self.blockUpComplete) {
  164. self.blockUpComplete(arr);
  165. }
  166. // [self resetView];
  167. [self.collectionView reloadData];
  168. }
  169. - (UICollectionView *)collectionView{
  170. if (_collectionView == nil) {
  171. // 1.流水布局
  172. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  173. layout.minimumInteritemSpacing = 1;
  174. layout.minimumLineSpacing = 1;
  175. layout.sectionInset = UIEdgeInsetsMake(1, W(7), 0, W(5));
  176. CGSize size = [self fetchCellSize];
  177. layout.itemSize = size;
  178. layout.scrollDirection = self.scrollDirection;
  179. // 6.创建UICollectionView
  180. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height) collectionViewLayout:layout];
  181. // 7.设置collectionView的背景色
  182. _collectionView.backgroundColor = [UIColor whiteColor];
  183. // 8.设置代理
  184. _collectionView.collectionViewLayout = layout;
  185. _collectionView.delegate = self;
  186. _collectionView.dataSource = self;
  187. _collectionView.scrollEnabled = YES;
  188. _collectionView.showsVerticalScrollIndicator = false;
  189. _collectionView.showsHorizontalScrollIndicator = false;
  190. [_collectionView registerClass:NSClassFromString(self.collectionCellName) forCellWithReuseIdentifier:self.collectionCellName];
  191. }
  192. return _collectionView;
  193. }
  194. #pragma mark 初始化
  195. - (instancetype)init{
  196. return [self initWithConfig:true];
  197. }
  198. - (instancetype)initWithConfig:(BOOL)config{
  199. self = [super init];
  200. if (self) {
  201. //default
  202. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  203. self.isShowTitleBottom = false;
  204. self.isEditing = true;
  205. self.backgroundColor = [UIColor clearColor];
  206. self.widthHeight = XY(SCREEN_WIDTH, 0);
  207. self.collectionCellName = @"CollectionImageCell";
  208. //config
  209. if (config) {
  210. [self configView];
  211. }
  212. }
  213. return self;
  214. }
  215. - (void)configView{
  216. [self resetView];
  217. [self addSubview:self.collectionView];
  218. self.collectionView.contentInset = UIEdgeInsetsMake(W(1), W(10), 0, self.scrollDirection == UICollectionViewScrollDirectionVertical?W(20):0);
  219. }
  220. -(void)resetView{
  221. CGSize size = [self fetchCellSize];
  222. NSInteger count = self.assets.count;
  223. self.height= count%3 == 0 ? count/3 * size.height + count/3 :count/3 * size.height + count/3 + size.height;
  224. self.collectionView.height = self.height;
  225. [self.collectionView reloadData];
  226. // self.height = (sizeCell.height+W(15))*self.assets.count;
  227. }
  228. #pragma mark fetch cell size
  229. - (CGSize)fetchCellSize{
  230. return self.isShowTitleBottom?[NSClassFromString(self.collectionCellName) fetchHeightWithTitle]:[NSClassFromString(self.collectionCellName) fetchHeight];
  231. }
  232. @end