JCHATAlbumViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // HMAlbumViewController.m
  3. // photosFramework
  4. //
  5. // Created by HuminiOS on 15/11/11.
  6. // Copyright © 2015年 HuminiOS. All rights reserved.
  7. //
  8. #import "JCHATAlbumViewController.h"
  9. #import <Photos/Photos.h>
  10. #import <AssetsLibrary/AssetsLibrary.h>
  11. #import "JCHATAlbumTableViewCell.h"
  12. #import "JCHATAlbumModel.h"
  13. @interface JCHATAlbumViewController ()<UITableViewDelegate,UITableViewDataSource>
  14. @property (weak, nonatomic) IBOutlet UITableView *albumTable;
  15. @property (strong, nonatomic) NSMutableArray * assetsGroups; //iOS 8以前 的数据 的
  16. @property (strong, nonatomic)NSMutableArray *albumArr;
  17. @property (assign, nonatomic)NSInteger getDataschedule;// iOS 8 以前用来标识 所有的数据 已经获取完成
  18. @end
  19. @implementation JCHATAlbumViewController
  20. - (void)viewDidLoad {// add model
  21. [super viewDidLoad];
  22. self.navigationController.navigationBar.hidden = YES;
  23. self.view.backgroundColor = Normal_Color;
  24. self.albumTable.backgroundColor = Normal_Color;
  25. self.albumTable.separatorStyle = UITableViewCellSeparatorStyleNone;
  26. self.tabelTop.constant = 64+statusbarHeight;
  27. self.rightBtn.hidden = NO;
  28. self.titleL.text = YZMsg(@"相簿");
  29. [self.rightBtn setTitle:YZMsg(@"取消") forState:0];
  30. _albumArr = @[].mutableCopy;
  31. if ([[[UIDevice currentDevice]systemVersion] floatValue]>= 8) {
  32. [self prepareAlbumArrWithPhotosFramework];
  33. } else {
  34. [self prepareAlbumArrWithAssert];
  35. }
  36. [_albumTable registerNib:[UINib nibWithNibName:@"JCHATAlbumTableViewCell" bundle:nil]
  37. forCellReuseIdentifier:@"JCHATAlbumTableViewCell"];
  38. _albumTable.delegate = self;
  39. _albumTable.dataSource = self;
  40. _albumTable.tableFooterView = [UIView new];
  41. if ([[[UIDevice currentDevice]systemVersion] floatValue]>= 8) {
  42. [self pushToSelectPhotoVCWithIndex:0];
  43. } else {
  44. [self performSelector:@selector(pushToSelectPhotoVCWithIndex:) withObject:0 afterDelay:0.1];
  45. }
  46. }
  47. - (void)prepareAlbumArrWithPhotosFramework {//ios 8 系统以后使用 photos.framework
  48. // all photoAlbumdel
  49. PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
  50. allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
  51. PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
  52. JCHATAlbumModel *albumModel = [JCHATAlbumModel new];
  53. if (allPhotos.count > 0) {
  54. [albumModel setDataWithAlbumResult:allPhotos];
  55. [_albumArr addObject:albumModel];
  56. }
  57. // smartAlbums
  58. PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
  59. for (PHAssetCollection *enumCollection in smartAlbums) {
  60. PHFetchResult *albumImagaAssert = [PHAsset fetchAssetsInAssetCollection:enumCollection options:nil];
  61. if (albumImagaAssert.count == 0) {
  62. continue;
  63. }
  64. JCHATAlbumModel *model = [JCHATAlbumModel new];
  65. [model setDataWithAlbumCollection:enumCollection];
  66. [_albumArr addObject:model];
  67. }
  68. PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
  69. for (PHAssetCollection *enumCollection in topLevelUserCollections) {
  70. PHFetchResult *albumImagaAssert = [PHAsset fetchAssetsInAssetCollection:enumCollection options:nil];
  71. if (albumImagaAssert.count == 0) {
  72. continue;
  73. }
  74. JCHATAlbumModel *model = [JCHATAlbumModel new];
  75. [model setDataWithAlbumCollection:enumCollection];
  76. [_albumArr addObject:model];
  77. }
  78. }
  79. - (void)prepareAlbumArrWithAssert {//ios 8 以前使用 AssetsLibrary
  80. ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
  81. void (^assetsGroupsEnumerationBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *assetsGroup, BOOL *stop) {
  82. if(assetsGroup) {
  83. _getDataschedule --;
  84. [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
  85. if(assetsGroup.numberOfAssets > 0) {
  86. JCHATAlbumModel *model = [JCHATAlbumModel new];
  87. [model setDataWithAssets:assetsGroup];
  88. [_albumArr addObject:model];
  89. }
  90. if (_getDataschedule == 0) {
  91. [self.albumTable reloadData];//移出去
  92. }
  93. }
  94. };
  95. void (^assetsGroupsFailureBlock)(NSError *) = ^(NSError *error) {
  96. _getDataschedule --;
  97. if (_getDataschedule == 0) {
  98. [self.albumTable reloadData];
  99. }
  100. NSLog(@"Error: %@", [error localizedDescription]);
  101. };
  102. _getDataschedule = 4;
  103. // Enumerate Camera Roll
  104. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  105. // Album
  106. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  107. // Event
  108. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupEvent usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  109. // Faces
  110. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupFaces usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  111. }
  112. - (void)cancel{
  113. [self dismissViewControllerAnimated:YES completion:nil];
  114. }
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  116. return 1;
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  119. return _albumArr.count;
  120. }
  121. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  122. if (indexPath.section != 0) { //如果相册没有图片 则不显示,
  123. PHFetchResult *albumResult = _albumArr[indexPath.section];
  124. PHFetchResult *allPhotoInCollection = [PHAsset fetchAssetsInAssetCollection:(PHAssetCollection *)albumResult[indexPath.row] options:nil];
  125. if (allPhotoInCollection.count == 0) {
  126. return 0;
  127. }
  128. }
  129. return 55+16;
  130. }
  131. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  132. static NSString *albumCellIdentify = @"JCHATAlbumTableViewCell";
  133. JCHATAlbumTableViewCell *cell = (JCHATAlbumTableViewCell *)[_albumTable dequeueReusableCellWithIdentifier:albumCellIdentify];
  134. [cell layoutWithAlbumModel:_albumArr[indexPath.row]];
  135. cell.backgroundColor = CellRow_Cor;
  136. cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:SelCell_Col]];
  137. return cell;
  138. }
  139. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  140. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  141. [self pushToSelectPhotoVCWithIndex:indexPath.row];
  142. }
  143. - (void)pushToSelectPhotoVCWithIndex:(NSInteger)index {
  144. JCHATPhotoSelectViewController *selectPhotoVC = [[JCHATPhotoSelectViewController alloc] init];
  145. if(_albumArr.count == 0) return;
  146. JCHATAlbumModel *model = _albumArr[index];
  147. if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 8) {
  148. if (index == 0) {
  149. selectPhotoVC.allFetchResult = model.albumFetchResult;
  150. } else {
  151. selectPhotoVC.photoCollection = model.albumCollection;
  152. }
  153. } else {
  154. selectPhotoVC.assetsGroup = model.assetsGroup;
  155. }
  156. selectPhotoVC.photoDelegate = _photoDelegate;
  157. [self.navigationController pushViewController:selectPhotoVC animated:NO];
  158. }
  159. - (void)didReceiveMemoryWarning {
  160. [super didReceiveMemoryWarning];
  161. // Dispose of any resources that can be recreated.
  162. }
  163. /*
  164. #pragma mark - Navigation
  165. // In a storyboard-based application, you will often want to do a little preparation before navigation
  166. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  167. // Get the new view controller using [segue destinationViewController].
  168. // Pass the selected object to the new view controller.
  169. }
  170. */
  171. #pragma mark - 导航
  172. - (void)clickNaviRightBtn {
  173. [self dismissViewControllerAnimated:YES completion:nil];
  174. }
  175. @end