JCHATPhotoSelectViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // HMPhotoSelectViewController.m
  3. // HMPhotoPickerDemo
  4. //
  5. // Created by HuminiOS on 15/11/16.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "JCHATPhotoSelectViewController.h"
  9. #import "ThumbImageCollectionViewCell.h"
  10. #import "JCHATPhotoBrowserViewController.h"
  11. #import "JCHATPhotoModel.h"
  12. #import "JCHATPhotoPickerConstants.h"
  13. #import "AppDelegate.h"
  14. #import "JCHATSelectImgCollectionView.h"
  15. #define kPhotoGridViewFrame CGRectMake(0, 0, screenWidth,screenHeight - 45)
  16. #define kBrowserBtnFrame CGRectMake(13, 10, 35, 16)
  17. #define kSendBtnFrame CGRectMake(screenWidth - 45, 10, 35, 16)
  18. @interface JCHATPhotoSelectViewController (){
  19. PHCachingImageManager *imageManager;
  20. NSMutableDictionary *selectedPhotoDic;// 已经选中的图片
  21. NSMutableArray *allPhotoArr;
  22. __weak IBOutlet JCHATSelectImgCollectionView *photoGridView;
  23. __weak IBOutlet UIButton *browserBtn;
  24. __weak IBOutlet UILabel *selectedPhotoLabel;
  25. __weak IBOutlet UIButton *sendBtn;
  26. __weak IBOutlet UIView *bottomV;
  27. }
  28. @end
  29. @implementation JCHATPhotoSelectViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.navigationController.navigationBar.translucent = NO;
  33. self.navigationController.navigationBar.hidden = YES;
  34. bottomV.backgroundColor = RGB_COLOR(@"#2c2840", 1);
  35. selectedPhotoDic = @{}.mutableCopy;
  36. allPhotoArr = @[].mutableCopy;
  37. // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消"
  38. // style:UIBarButtonItemStylePlain
  39. // target:self
  40. // action:@selector(cancel)];
  41. selectedPhotoLabel.layer.masksToBounds = YES;
  42. selectedPhotoLabel.layer.cornerRadius = selectedPhotoLabel.frame.size.height / 2;
  43. imageManager = [[PHCachingImageManager alloc] init];
  44. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  45. // [defaultCenter addObserver:self selector:@selector(didSelectStatusChange:) name:kSelectStatusChange object:nil];
  46. // [defaultCenter addObserver:self selector:@selector(finshToSelectPhoto:) name:kFinishToSelectPhoto object:nil];
  47. self.titleL.text = YZMsg(@"相机胶卷");
  48. self.rightBtn.hidden = NO;
  49. [self.rightBtn setTitle:YZMsg(@"取消") forState:0];
  50. [self setUpCollectionView];
  51. if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 8) {
  52. [self getAllPhotoWithPhotosFramework];
  53. } else {
  54. [self getAllPhotoWithAssert];
  55. }
  56. }
  57. - (void)cancel{
  58. [self dismissViewControllerAnimated:YES completion:nil];
  59. }
  60. - (void)getAllPhotoWithPhotosFramework {
  61. if (_allFetchResult == nil) {
  62. _allFetchResult = [PHAsset fetchAssetsInAssetCollection:(PHAssetCollection *)_photoCollection options:nil];
  63. }
  64. if (_photoCollection.localIdentifier == nil) {
  65. self.titleL.text = YZMsg(@"相机胶卷");
  66. } else {
  67. self.titleL.text = _photoCollection.localizedTitle;
  68. }
  69. PHFetchResult *allPhotoResult = _allFetchResult;
  70. for (int index = 0; index < [allPhotoResult count]; index ++) {
  71. PHAsset *asset = allPhotoResult[index];
  72. JCHATPhotoModel *model = [[JCHATPhotoModel alloc] init];
  73. [model setDataWithPhotoAsset:asset imageManager:imageManager];
  74. [allPhotoArr addObject:model];
  75. }
  76. [photoGridView reloadData];
  77. [self performSelector:@selector(scrollPhotoGridToBottom) withObject:nil afterDelay:0.1];
  78. }
  79. - (void)scrollPhotoGridToBottom {
  80. if (allPhotoArr.count == 0) return;
  81. [photoGridView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:allPhotoArr.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  82. }
  83. - (void)getAllPhotoWithAssert {
  84. [[[ALAssetsLibrary alloc] init] enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  85. if (group != nil) {
  86. //设置过滤对象
  87. ALAssetsFilter *filter = [ALAssetsFilter allPhotos];
  88. [group setAssetsFilter:filter];
  89. //通过文件夹枚举遍历所有的相片ALAsset对象,有多少照片,则调用多少次block
  90. [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  91. if (result != nil) {
  92. //将result对象存储到数组中
  93. JCHATPhotoModel *model = [[JCHATPhotoModel alloc] init];
  94. [model setDatawithAsset:result];
  95. [allPhotoArr addObject:model];
  96. }
  97. }];
  98. // 将所有获取的 照片模型 交给manager统一管理
  99. [photoGridView reloadData];
  100. [photoGridView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:allPhotoArr.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  101. }
  102. } failureBlock:^(NSError *error) {
  103. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"提示",nil) message:NSLocalizedString(@"请允许访问相册,方可使用此功能!\n您可以在\"设置->隐私->照片\"中启用",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消",nil) otherButtonTitles:NSLocalizedString(@"设置",nil), nil];
  104. [alertView show];
  105. }];
  106. }
  107. - (void)setUpCollectionView {
  108. self.collectionTop.constant = 64+statusbarHeight;
  109. self.botToolHeigh.constant = 45+ShowDiff;
  110. photoGridView.minimumZoomScale = 0;
  111. photoGridView.contentOffset = CGPointMake(0, 64);
  112. [photoGridView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"gradientCell"];
  113. [photoGridView registerNib:[UINib nibWithNibName:@"ThumbImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ThumbImageCollectionViewCell"];
  114. [self.view addSubview:photoGridView];
  115. UICollectionViewFlowLayout *collectLayout = (UICollectionViewFlowLayout *)photoGridView.collectionViewLayout;
  116. collectLayout.itemSize = CGSizeMake(_window_width/4-1, _window_width/4-1);
  117. collectLayout.minimumLineSpacing = 0.5;
  118. collectLayout.minimumInteritemSpacing = 0.5;
  119. NSLog(@"translucent %@",[UINavigationBar appearance].translucent?@"yes":@"no");
  120. if ([UINavigationBar appearance].translucent == YES) {
  121. collectLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 64);
  122. }
  123. photoGridView.backgroundColor = Normal_Color;
  124. photoGridView.delegate = self;
  125. photoGridView.dataSource = self;
  126. [photoGridView reloadData];
  127. }
  128. - (IBAction)clickToBrowserSelectedPhotos:(id)sender {
  129. JCHATPhotoBrowserViewController *photoBrowserVC = [[JCHATPhotoBrowserViewController alloc] init];
  130. photoBrowserVC.photoCollection = _photoCollection;
  131. photoBrowserVC.imageManager = imageManager;
  132. photoBrowserVC.allFetchResult = _allFetchResult;
  133. photoBrowserVC.photoDelegate = _photoDelegate;
  134. photoBrowserVC.selectVCDelegate = self;
  135. photoBrowserVC.allPhotoArr = [NSMutableArray arrayWithArray:[selectedPhotoDic allValues]];
  136. NSIndexPath *browserIndex= [NSIndexPath indexPathForItem:0 inSection:0];
  137. photoBrowserVC.currentIndex = browserIndex;
  138. [self.navigationController pushViewController:photoBrowserVC animated:YES];
  139. }
  140. - (IBAction)sendSelectedPhotos:(id)sender {
  141. [self finshToSelectPhoto:nil];
  142. }
  143. - (void)finshToSelectPhoto:(JCHATPhotoModel *)model {
  144. // if (notification != nil) {
  145. // if (selectedPhotoDic.count == 0) {
  146. // HMPhotoModel *model = notification.object;
  147. // if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 8) {//宏
  148. // [selectedPhotoDic setObject:model forKey:model.photoAsset];
  149. // } else {
  150. // [selectedPhotoDic setObject:model forKey:model.imgURL];
  151. // }
  152. // }
  153. // }
  154. if (selectedPhotoDic.count == 0) {
  155. if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 8) {//宏
  156. [selectedPhotoDic setObject:model forKey:model.photoAsset];
  157. } else {
  158. [selectedPhotoDic setObject:model forKey:model.imgURL];
  159. }
  160. }
  161. if ([_photoDelegate respondsToSelector:@selector(JCHATPhotoPickerViewController:selectedPhotoArray:)]){
  162. __block NSMutableArray *selectedImageArr = @[].mutableCopy;
  163. for (NSString *key in selectedPhotoDic) {
  164. JCHATPhotoModel *photoModel = selectedPhotoDic[key];
  165. if (photoModel.largeImage == nil) {
  166. NSLog(@"fail to get large image");
  167. break;
  168. }
  169. [selectedImageArr addObject:photoModel.largeImage];
  170. }
  171. [_photoDelegate JCHATPhotoPickerViewController:self selectedPhotoArray:selectedImageArr];
  172. }
  173. [self dismissViewControllerAnimated:YES completion:NULL];
  174. }
  175. - (void)didSelectStatusChange:(JCHATPhotoModel *)model {
  176. if ([[[UIDevice currentDevice]systemVersion] floatValue]>= 8) {
  177. if (selectedPhotoDic[model.photoAsset] == nil) {
  178. if (selectedPhotoDic.count > 8) {
  179. [MBProgressHUD showPop:YZMsg(@"最多选择9张图片")];
  180. return;
  181. }
  182. [selectedPhotoDic setObject:model forKey:model.photoAsset];
  183. } else {
  184. [selectedPhotoDic removeObjectForKey:model.photoAsset];
  185. }
  186. } else {
  187. if (selectedPhotoDic[model.asset] == nil) {
  188. if (selectedPhotoDic.count > 8) {
  189. [MBProgressHUD showPop:YZMsg(@"最多选择9张图片")];
  190. return;
  191. }
  192. [selectedPhotoDic setObject:model forKey:model.imgURL];
  193. } else {
  194. [selectedPhotoDic removeObjectForKey:model.imgURL];
  195. }
  196. }
  197. if ([selectedPhotoDic count] > 0) {
  198. browserBtn.enabled = YES;
  199. [browserBtn setTitleColor:Pink_Cor forState:0];
  200. selectedPhotoLabel.hidden = NO;
  201. selectedPhotoLabel.text = [NSString stringWithFormat:@"%ld",(unsigned long)[selectedPhotoDic count]];
  202. sendBtn.enabled = YES;
  203. [sendBtn setTitleColor:Pink_Cor forState:0];
  204. } else {
  205. browserBtn.enabled = NO;
  206. [browserBtn setTitleColor:RGB_COLOR(@"#8c8c8c", 0.6) forState:0];
  207. selectedPhotoLabel.hidden = YES;
  208. sendBtn.enabled = NO;
  209. [sendBtn setTitleColor:RGB_COLOR(@"#8c8c8c", 0.6) forState:0];
  210. }
  211. }
  212. - (void)viewWillAppear:(BOOL)animated {
  213. [self.navigationController setNavigationBarHidden:YES];
  214. [photoGridView reloadData];
  215. }
  216. #pragma mark - CollectionViewDelegate
  217. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  218. return allPhotoArr.count;
  219. }
  220. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  221. return 1;
  222. }
  223. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  224. // return CGSizeMake(80, 80);
  225. //}
  226. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  227. static NSString *CellIdentifier = @"ThumbImageCollectionViewCell";
  228. ThumbImageCollectionViewCell *cell = (ThumbImageCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  229. [cell setDataWithModel:allPhotoArr[indexPath.item] withDelegate:self];
  230. cell.backgroundColor = CellRow_Cor;
  231. return cell;
  232. }
  233. - (void)collectionView:(UICollectionView * _Nonnull)collectionView
  234. didSelectItemAtIndexPath:(NSIndexPath * _Nonnull)indexPath {
  235. JCHATPhotoBrowserViewController *photoBrowserVC = [[JCHATPhotoBrowserViewController alloc] init];
  236. photoBrowserVC.photoCollection = _photoCollection;
  237. photoBrowserVC.imageManager = imageManager;
  238. photoBrowserVC.allFetchResult = _allFetchResult;
  239. photoBrowserVC.allPhotoArr = allPhotoArr;
  240. photoBrowserVC.photoDelegate = _photoDelegate;
  241. photoBrowserVC.currentIndex = indexPath;
  242. photoBrowserVC.selectVCDelegate = self;
  243. [self.navigationController pushViewController:photoBrowserVC animated:YES];
  244. }
  245. - (void)didReceiveMemoryWarning {
  246. [super didReceiveMemoryWarning];
  247. }
  248. #pragma mark - 导航
  249. - (void)clickNaviRightBtn {
  250. [self dismissViewControllerAnimated:YES completion:nil];
  251. }
  252. @end