GWLPhotoGroupDetailController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // GWLPhotoGroupDetailController.m
  3. // GWLPhotoSelector
  4. //
  5. // Created by GaoWanli on 15/7/23.
  6. // Copyright (c) 2015年 GWL. All rights reserved.
  7. //
  8. #import "GWLPhotoGroupDetailController.h"
  9. @interface GWLPhotoCell : UICollectionViewCell
  10. @property(nonatomic, strong) GWLPhotoALAssets *photoALAsset;
  11. @property(nonatomic, weak) UIImageView *iconView;
  12. @property(nonatomic, weak) UIView *selectedView;
  13. @property(nonatomic, weak) UIButton *selectedBtn;
  14. @end
  15. @implementation GWLPhotoCell
  16. - (void)setPhotoALAsset:(GWLPhotoALAssets *)photoALAsset {
  17. _photoALAsset = photoALAsset;
  18. if (kGWLPhotoSelector_Above_iOS8) {
  19. [self imageWithAsset:photoALAsset.photoAsset completion:^(UIImage *image) {
  20. self.iconView.image = image;
  21. }];
  22. }else {
  23. ALAsset *alasset = photoALAsset.photoALAsset;
  24. if ([[alasset valueForProperty:@"ALAssetPropertyType"] isEqualToString:ALAssetTypePhoto]) {
  25. self.iconView.image = [UIImage imageWithCGImage:alasset.thumbnail];
  26. }
  27. }
  28. self.selectedView.hidden = !photoALAsset.isSelected;
  29. self.selectedBtn.hidden = !photoALAsset.isSelected;
  30. }
  31. - (UIImageView *)iconView {
  32. if (!_iconView) {
  33. UIImageView *iconView = [[UIImageView alloc]initWithFrame:self.bounds];
  34. iconView.contentMode = UIViewContentModeScaleAspectFill;
  35. iconView.clipsToBounds = YES;
  36. [self.contentView addSubview:iconView];
  37. _iconView = iconView;
  38. }
  39. return _iconView;
  40. }
  41. - (UIView *)selectedView {
  42. if (!_selectedView) {
  43. UIView *selectedView = [[UIView alloc]initWithFrame:self.bounds];
  44. selectedView.backgroundColor = [UIColor whiteColor];
  45. selectedView.alpha = 0.2f;
  46. [self.iconView addSubview:selectedView];
  47. _selectedView = selectedView;
  48. }
  49. return _selectedView;
  50. }
  51. - (UIButton *)selectedBtn {
  52. if (!_selectedBtn) {
  53. UIButton *selectedBtn = [[UIButton alloc]init];
  54. selectedBtn.userInteractionEnabled = NO;
  55. NSString *imageFile = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"GWLPhotoSelector.bundle/sel@2x.png"];
  56. [selectedBtn setImage:[UIImage imageWithContentsOfFile:imageFile] forState:UIControlStateNormal];
  57. CGFloat margin = 5;
  58. selectedBtn.frame = CGRectMake(0, 0, selectedBtn.currentImage.size.width, selectedBtn.currentImage.size.height);
  59. CGFloat selectedBtnX = CGRectGetWidth(_iconView.frame) - CGRectGetWidth(selectedBtn.frame) - margin;
  60. CGFloat selectedBtnY = CGRectGetHeight(_iconView.frame) - CGRectGetHeight(selectedBtn.frame) - margin;
  61. selectedBtn.frame = CGRectMake(selectedBtnX, selectedBtnY, selectedBtn.currentImage.size.width, selectedBtn.currentImage.size.height);
  62. [self.iconView addSubview:selectedBtn];
  63. _selectedBtn = selectedBtn;
  64. }
  65. return _selectedBtn;
  66. }
  67. - (void)imageWithAsset:(PHAsset *)asset completion:(kGWLPhotoSelector_imageBlock)completion {
  68. PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
  69. imageOptions.synchronous = YES;
  70. [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(self.bounds.size.width * 2, self.bounds.size.height * 2) contentMode:PHImageContentModeAspectFill options:imageOptions resultHandler:^(UIImage *result, NSDictionary *info) {
  71. completion(result);
  72. }];
  73. }
  74. @end
  75. @interface GWLPhotoGroupDetailController ()
  76. @property(nonatomic, strong) NSMutableArray *selectedPhotos;
  77. @property(nonatomic, assign) NSInteger photosCount;
  78. @property(nonatomic, strong) NSMutableArray *imageArray;
  79. @end
  80. @implementation GWLPhotoGroupDetailController
  81. static NSString * const reuseIdentifier = @"gwlPhotoCell";
  82. - (instancetype)init {
  83. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  84. CGFloat itemW = ([UIScreen mainScreen].bounds.size.width - kGWLPhotoSelector_RowPhotoCount * 2) / kGWLPhotoSelector_RowPhotoCount;
  85. layout.itemSize = CGSizeMake(itemW, itemW);
  86. layout.minimumInteritemSpacing = 1;
  87. layout.minimumLineSpacing = 1;
  88. return [super initWithCollectionViewLayout:layout];
  89. }
  90. - (void)viewDidLoad {
  91. [super viewDidLoad];
  92. self.collectionView.backgroundColor = [UIColor whiteColor];
  93. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneBtnClick:)];
  94. [self.collectionView registerClass:[GWLPhotoCell class] forCellWithReuseIdentifier:reuseIdentifier];
  95. }
  96. - (void)viewWillAppear:(BOOL)animated {
  97. [super viewWillAppear:animated];
  98. [self setupTitle];
  99. }
  100. - (void)doneBtnClick:(UIBarButtonItem *)doneBarButtonItem {
  101. doneBarButtonItem.enabled = NO;
  102. __weak typeof (self) selfVc = self;
  103. [[[NSOperationQueue alloc]init] addOperationWithBlock:^{
  104. for (GWLPhotoALAssets *photoALAsset in self.selectedPhotos) {
  105. if (kGWLPhotoSelector_Above_iOS8) {
  106. [selfVc imageWithAsset:photoALAsset.photoAsset completion:^(UIImage *image) {
  107. [selfVc.imageArray addObject:image];
  108. }];
  109. }else {
  110. ALAsset *sset = photoALAsset.photoALAsset;
  111. ALAssetRepresentation *assetRepresentation = [sset defaultRepresentation];
  112. CGFloat imageScale = [assetRepresentation scale];
  113. UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];
  114. UIImage *originalImage = [UIImage imageWithCGImage:sset.defaultRepresentation.fullScreenImage scale:imageScale orientation:imageOrientation];
  115. [selfVc.imageArray addObject:originalImage];
  116. }
  117. }
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. if (selfVc.block) {
  120. [selfVc dismissViewControllerAnimated:YES completion:^{
  121. selfVc.block(selfVc.imageArray);
  122. selfVc.selectedPhotos = nil;
  123. selfVc.imageArray = nil;
  124. }];
  125. }else {
  126. selfVc.selectedPhotos = nil;
  127. selfVc.imageArray = nil;
  128. }
  129. });
  130. }];
  131. }
  132. - (void)imageWithAsset:(PHAsset *)asset completion:(kGWLPhotoSelector_imageBlock)completion {
  133. PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
  134. imageOptions.synchronous = YES;
  135. [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight) contentMode:PHImageContentModeAspectFill options:imageOptions resultHandler:^(UIImage *result, NSDictionary *info) {
  136. completion(result);
  137. }];
  138. }
  139. - (void)setPhotoALAssets:(NSArray *)photoALAssets {
  140. _photoALAssets = photoALAssets;
  141. if (photoALAssets.count > 0)
  142. self.photosCount = photoALAssets.count - 1;
  143. [self.collectionView reloadData];
  144. }
  145. - (NSMutableArray *)selectedPhotos {
  146. if (!_selectedPhotos)
  147. _selectedPhotos = [NSMutableArray array];
  148. return _selectedPhotos;
  149. }
  150. #pragma mark <UICollectionViewDataSource>
  151. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  152. return self.photoALAssets.count;
  153. }
  154. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  155. GWLPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  156. GWLPhotoALAssets *photoALAssets = self.photoALAssets[self.photosCount - indexPath.row];
  157. cell.photoALAsset = photoALAssets;
  158. return cell;
  159. }
  160. #pragma mark <UICollectionViewDelegate>
  161. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  162. GWLPhotoALAssets *photoALAsset = self.photoALAssets[self.photosCount - indexPath.row];
  163. photoALAsset.selected = !photoALAsset.isSelected;
  164. if (photoALAsset.isSelected) {
  165. [self.selectedPhotos addObject:photoALAsset];
  166. if (self.selectedPhotos.count > self.maxCount) {
  167. [self.selectedPhotos removeLastObject];
  168. photoALAsset.selected = NO;
  169. }
  170. }
  171. else {
  172. [self.selectedPhotos removeObject:photoALAsset];
  173. }
  174. [self setupTitle];
  175. [collectionView reloadItemsAtIndexPaths:@[indexPath]];
  176. }
  177. - (void)setupTitle {
  178. self.title = [NSString stringWithFormat:@"%zd / %zd", self.selectedPhotos.count, self.maxCount];
  179. }
  180. #pragma mark - getter && setter
  181. - (NSMutableArray *)imageArray {
  182. if (!_imageArray) {
  183. _imageArray = [NSMutableArray array];
  184. }
  185. return _imageArray;
  186. }
  187. @end