ImagePickerVC.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // ImagePickerVC.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/4/6.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "ImagePickerVC.h"
  9. //cell
  10. #import "ImagePickerCollectionCell.h"
  11. //下拉列表
  12. //#import "DownListViewBG.h"
  13. //相册权限
  14. #import <Photos/Photos.h>
  15. @interface ImagePickerVC ()
  16. @property (strong, nonatomic) NSMutableArray *groupArray;
  17. @end
  18. @implementation ImagePickerVC
  19. #pragma mark lazy init
  20. - (ImagePickerBottomView *)bottomView{
  21. if (!_bottomView) {
  22. _bottomView = [ImagePickerBottomView initWithModel:nil];
  23. _bottomView.bottom = SCREEN_HEIGHT;
  24. [_bottomView.btnComplete addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
  25. [_bottomView.btnSelectAlbum addTarget:self action:@selector(showSelectList) forControlEvents:UIControlEventTouchUpInside];
  26. }
  27. return _bottomView;
  28. }
  29. - (ImagePikcerListBGVIew *)viewListBG{
  30. if (!_viewListBG) {
  31. _viewListBG = [ImagePikcerListBGVIew new];
  32. WEAKSELF
  33. _viewListBG.blockSelect = ^(PHAssetCollection *group){
  34. [weakSelf getImageWithGroup:group];
  35. };
  36. }
  37. return _viewListBG;
  38. }
  39. - (NSMutableArray *)groupArray{
  40. if (!_groupArray) {
  41. _groupArray = [NSMutableArray array];
  42. }
  43. return _groupArray;
  44. }
  45. - (NSMutableArray *)selectAssetArray{
  46. if (!_selectAssetArray) {
  47. _selectAssetArray = [NSMutableArray array];
  48. }
  49. return _selectAssetArray;
  50. }
  51. - (NSMutableArray *)imageAssetAray{
  52. if (!_imageAssetAray) {
  53. _imageAssetAray = [NSMutableArray array];
  54. }
  55. return _imageAssetAray;
  56. }
  57. - (BaseNavView *)navView{
  58. if (!_navView) {
  59. _navView = [BaseNavView initNavBackTitle:@"" rightView:nil];
  60. _navView.blockBack = ^{
  61. [GB_Nav popViewControllerAnimated:true];
  62. };
  63. }
  64. return _navView;
  65. }
  66. - (UICollectionView *)collectionView{
  67. if (_collectionView == nil) {
  68. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  69. //layout
  70. layout.minimumInteritemSpacing = 1;
  71. layout.minimumLineSpacing = 1;
  72. layout.itemSize = CGSizeMake(SCREEN_WIDTH / 3.0 - 1, SCREEN_WIDTH / 3.0 - 1 );
  73. //direction
  74. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  75. //collection
  76. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-NAVIGATIONBAR_HEIGHT - self.bottomView.height) collectionViewLayout:layout];
  77. // backgroundColor
  78. _collectionView.backgroundColor = [UIColor clearColor];
  79. // delegate
  80. _collectionView.collectionViewLayout = layout;
  81. _collectionView.delegate = self;
  82. _collectionView.dataSource = self;
  83. _collectionView.scrollEnabled = YES;
  84. _collectionView.showsVerticalScrollIndicator = false;
  85. _collectionView.showsHorizontalScrollIndicator = false;
  86. [_collectionView registerClass:[ImagePickerCollectionCell class] forCellWithReuseIdentifier:@"ImagePickerCollectionCell"];
  87. }
  88. return _collectionView;
  89. }
  90. - (PHAssetMediaType)assetType{
  91. return PHAssetMediaTypeImage;
  92. }
  93. #pragma mark view appear
  94. - (void)viewWillAppear:(BOOL)animated
  95. {
  96. [super viewWillAppear:animated];
  97. // self.navigationController.navigationBarHidden = true;
  98. // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  99. [self.collectionView reloadData];
  100. }
  101. - (void)viewDidLoad
  102. {
  103. [super viewDidLoad];
  104. [self.view addSubview:self.collectionView];
  105. [self.view addSubview:self.bottomView];
  106. [self.view addSubview:self.navView];
  107. [self.view addSubview:self.viewListBG];
  108. WEAKSELF
  109. [GlobalMethod fetchPhotoAuthorityBlock:^{
  110. [weakSelf getAlbumList];
  111. }];
  112. }
  113. //获取相册列表
  114. - (void)getAlbumList
  115. {
  116. BOOL isFetchCamera = false;
  117. PHFetchOptions *options = [[PHFetchOptions alloc] init];
  118. //只要图片
  119. options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",self.assetType];
  120. // 按图片生成时间排序
  121. options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
  122. // 所有智能相册
  123. PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
  124. for (PHAssetCollection *collection in smartAlbums) {
  125. //遍历获取相册
  126. if (![collection isKindOfClass:[PHAssetCollection class]])continue;
  127. //过滤最近删除
  128. if (collection.assetCollectionSubtype >= 212) continue;
  129. //获取相册内asset result
  130. PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsInAssetCollection:collection options:options];
  131. if (!result.count) continue;
  132. if (collection.assetCollectionSubtype == 209) {
  133. isFetchCamera = true;
  134. [self getImageWithGroup:collection];
  135. }
  136. [self.groupArray addObject:collection];
  137. }
  138. if (!isFetchCamera) {
  139. [self getImageWithGroup:self.groupArray.firstObject];
  140. }
  141. }
  142. //根据相册获取下面的图片
  143. - (void)getImageWithGroup:(PHAssetCollection *)assetCollection
  144. {
  145. if (!assetCollection) return;
  146. //清空数据
  147. [self.bottomView.btnComplete setTitle:@"确定" forState:UIControlStateNormal];
  148. // [self.selectAssetArray removeAllObjects];
  149. [self.imageAssetAray removeAllObjects];
  150. //更改navTitle
  151. NSString *name = UnPackStr(assetCollection.localizedTitle);
  152. [self.navView changeTitle:name];
  153. [GlobalMethod asynthicBlock:^{
  154. PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:^(){
  155. PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
  156. // 按图片生成时间排序
  157. allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:NO]];
  158. return allPhotosOptions;
  159. }()];
  160. if (fetchResult.count != 0) {
  161. for (NSInteger j = 0; j < fetchResult.count; j++) {
  162. //从相册中取出照片
  163. [self.imageAssetAray addObject:fetchResult[j]];
  164. }
  165. }
  166. WEAKSELF
  167. dispatch_async(dispatch_get_main_queue(), ^{
  168. [weakSelf.collectionView reloadData];
  169. });
  170. }];
  171. }
  172. #pragma mark--UICollectionViewDataSource,UICollectionViewDelegate
  173. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  174. {
  175. return self.imageAssetAray.count + 1;
  176. }
  177. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. ImagePickerCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImagePickerCollectionCell" forIndexPath:indexPath];
  180. //selectBtnClick
  181. if (indexPath.row !=0)
  182. {
  183. //缩略图 aspectRatioThumbnail thumbnail
  184. [cell resetCellWithImage:self.imageAssetAray[indexPath.row - 1] isSelected:[self.selectAssetArray containsObject:self.imageAssetAray[indexPath.row - 1]]];
  185. WEAKSELF
  186. cell.blockClick = ^(ImagePickerCollectionCell *cell){
  187. if (weakSelf.selectAssetArray.count >= weakSelf.photoNumber && !cell.rightButton.selected) {
  188. if (weakSelf.photoNumber == 1) {
  189. //如果一张 那就不停地替换
  190. [weakSelf.selectAssetArray removeAllObjects];
  191. [weakSelf.selectAssetArray addObject:weakSelf.imageAssetAray[indexPath.row - 1]];
  192. [weakSelf resetBottomView];
  193. [weakSelf.collectionView reloadData];
  194. return ;
  195. }else {
  196. [weakSelf showAlert];
  197. return ;
  198. }
  199. }
  200. cell.rightButton.selected = !cell.rightButton.selected;
  201. NSIndexPath *indexPath = [weakSelf.collectionView indexPathForCell:cell];
  202. if (cell.rightButton.selected) {
  203. //加入
  204. [weakSelf.selectAssetArray addObject:weakSelf.imageAssetAray[indexPath.row - 1]];
  205. }else{
  206. [weakSelf.selectAssetArray removeObject:weakSelf.imageAssetAray[indexPath.row - 1]];
  207. }
  208. [weakSelf resetBottomView];
  209. };
  210. }
  211. else
  212. {
  213. [cell resetCellWithImage:nil isSelected:false];
  214. cell.iconImg.image = [UIImage imageNamed:@"ico_相机"];
  215. cell.rightButton.hidden = true;
  216. }
  217. return cell;
  218. }
  219. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  220. return CGSizeMake(SCREEN_WIDTH / 3.0 - 1, SCREEN_WIDTH / 3.0 - 1 );
  221. }
  222. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  223. {
  224. if (indexPath.row == 0)
  225. {
  226. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  227. {
  228. UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  229. imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  230. imagePicker.delegate = self;
  231. imagePicker.allowsEditing = YES;
  232. [self presentViewController:imagePicker animated:YES completion:nil];
  233. }
  234. else
  235. {
  236. [[[UIAlertView alloc] initWithTitle:@"亲,您的设备不支持照相机功能" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
  237. }
  238. }
  239. }
  240. - (void)showAlert
  241. {
  242. NSString * str = [NSString stringWithFormat:@"最多只能选择%ld张图片",self.photoNumber];
  243. NSString * cancelButtonTitle = NSLocalizedString(@"我知道了", nil);
  244. NSString * destructiveButtonTitle = NSLocalizedString(str, nil);
  245. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
  246. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  247. //NSLog(@"取消");
  248. }];
  249. UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
  250. }];
  251. [alertController addAction:destructiveAction];
  252. [alertController addAction:cancelAction];
  253. [self presentViewController:alertController animated:YES completion:nil];
  254. }
  255. - (void)okBtnClick
  256. {
  257. if (self.selectAssetArray.count > self.photoNumber) {
  258. [self showAlert];
  259. }else{
  260. if ([_delegate respondsToSelector:@selector(ImagePickerVC:finishClick:)])
  261. {
  262. [_delegate ImagePickerVC:self finishClick:self.selectAssetArray];
  263. }
  264. [GB_Nav popViewControllerAnimated:true];
  265. }
  266. }
  267. - (void)showSelectList{
  268. [self.viewListBG resetViewWithAry:self.groupArray height:SCREEN_HEIGHT - self.bottomView.height];
  269. [self.viewListBG showAnimation];
  270. }
  271. #pragma mark resetBottomView
  272. - (void)resetBottomView{
  273. if (self.selectAssetArray.count == 0)
  274. {
  275. [self.bottomView.btnComplete setTitle:@"确定" forState:UIControlStateNormal];
  276. }
  277. else
  278. {
  279. [self.bottomView.btnComplete setTitle:[NSString stringWithFormat:@"确定(%lu)",(unsigned long)[self.selectAssetArray count]] forState:UIControlStateNormal];
  280. }
  281. }
  282. #pragma mark--UIImagePickerControllerDelegate
  283. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  284. WEAKSELF
  285. [picker dismissViewControllerAnimated:YES completion:^{
  286. if ([weakSelf.delegate respondsToSelector:@selector(ImagePickerVC:firstImageClick:)])
  287. {
  288. [weakSelf.delegate ImagePickerVC:self firstImageClick: info[picker.allowsEditing?UIImagePickerControllerEditedImage:UIImagePickerControllerOriginalImage]];
  289. }
  290. }];
  291. }
  292. @end