GWLPhotoLibrayController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // GWLPhotoLibrayController.m
  3. // GWLPhotoSelector
  4. //
  5. // Created by GaoWanli on 15/7/23.
  6. // Copyright (c) 2015年 GWL. All rights reserved.
  7. //
  8. #import "GWLPhotoLibrayController.h"
  9. #import "GWLPhotoGroupTableViewController.h"
  10. @interface GWLPhotoLibrayController ()
  11. @property (nonatomic, copy) kGWLPhotoSelector_ArrayBlock block;
  12. @property(nonatomic, strong) GWLPhotoGroupTableViewController *photoGroupTableViewController;
  13. @property(nonatomic, strong) ALAssetsLibrary *library;
  14. @property(nonatomic, strong) NSMutableArray *photoGroupArray;
  15. @property (nonatomic, strong) PHImageRequestOptions *imageOptions;
  16. @end
  17. @implementation GWLPhotoLibrayController
  18. + (instancetype)photoLibrayControllerWithBlock:(kGWLPhotoSelector_ArrayBlock) block {
  19. return [[self alloc]initWithBlock:block];
  20. }
  21. - (instancetype)initWithBlock:(kGWLPhotoSelector_ArrayBlock) block {
  22. _block = [block copy];
  23. return [super initWithRootViewController:self.photoGroupTableViewController];
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. [self setupImagePickerController];
  28. }
  29. - (void)setupImagePickerController {
  30. __weak typeof (self) selfVc = self;
  31. if (kGWLPhotoSelector_Above_iOS8) {
  32. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  33. if (status != PHAuthorizationStatusAuthorized) {
  34. dispatch_async(dispatch_get_main_queue(), ^{
  35. [selfVc.photoGroupTableViewController showErrorMessageView];
  36. });
  37. }else {
  38. PHFetchResult *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
  39. [collections enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
  40. [selfVc photoGroupWithCollection:collection];
  41. }];
  42. collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
  43. [collections enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
  44. if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) {
  45. [selfVc photoGroupWithCollection:collection];
  46. }
  47. }];
  48. dispatch_async(dispatch_get_main_queue(), ^{
  49. selfVc.photoGroupTableViewController.photoGroupArray = selfVc.photoGroupArray;
  50. });
  51. }
  52. }];
  53. }else {
  54. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  55. ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error) {
  56. if (error != nil) {
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. [selfVc.photoGroupTableViewController showErrorMessageView];
  59. });
  60. }
  61. };
  62. [selfVc.library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *aLAssets, BOOL* stop){
  63. if (aLAssets != nil) {
  64. NSString *groupName = [aLAssets valueForProperty:ALAssetsGroupPropertyName];
  65. UIImage *posterImage = [UIImage imageWithCGImage:[aLAssets posterImage]];
  66. GWLPhotoGroup *photoGroup = [[GWLPhotoGroup alloc]init];
  67. photoGroup.groupName = groupName;
  68. photoGroup.groupIcon = posterImage;
  69. [selfVc.photoGroupArray addObject:photoGroup];
  70. [aLAssets enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
  71. if (result != NULL) {
  72. if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
  73. GWLPhotoALAssets *photoALAssets = [[GWLPhotoALAssets alloc]init];
  74. photoALAssets.photoALAsset = result;
  75. photoALAssets.selected = NO;
  76. [photoGroup.photoALAssets addObject:photoALAssets];
  77. }
  78. }
  79. }];
  80. dispatch_async(dispatch_get_main_queue(), ^{
  81. selfVc.photoGroupTableViewController.photoGroupArray = selfVc.photoGroupArray;
  82. });
  83. }
  84. } failureBlock:failureblock];
  85. });
  86. }
  87. }
  88. - (void)photoGroupWithCollection:(PHAssetCollection *)collection {
  89. [self coverImageWithCollection:collection completion:^(UIImage *image) {
  90. GWLPhotoGroup *photoGroup = [[GWLPhotoGroup alloc]init];
  91. photoGroup.groupName = collection.localizedTitle;
  92. photoGroup.groupIcon = image;
  93. [self.photoGroupArray addObject:photoGroup];
  94. [self photoGroupSetALAsset:photoGroup collection:collection];
  95. }];
  96. }
  97. - (void)coverImageWithCollection:(PHAssetCollection *)collection completion:(kGWLPhotoSelector_imageBlock)completion {
  98. PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
  99. [assetResult enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
  100. if (asset.mediaType == PHAssetMediaTypeImage) {
  101. CGSize targetSize = CGSizeMake(kGWLPhotoSelector_Cell_Height, kGWLPhotoSelector_Cell_Height);
  102. [self imageWithAsset:asset targetSize:targetSize completion:^(UIImage *image) {
  103. completion(image);
  104. *stop = YES;
  105. }];
  106. }
  107. }];
  108. }
  109. - (void)photoGroupSetALAsset:(GWLPhotoGroup *)photoGroup collection:(PHAssetCollection *)collection {
  110. PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
  111. [assetResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
  112. if (asset.mediaType == PHAssetMediaTypeImage) {
  113. GWLPhotoALAssets *photoALAssets = [[GWLPhotoALAssets alloc]init];
  114. photoALAssets.photoAsset = asset;
  115. photoALAssets.selected = NO;
  116. [photoGroup.photoALAssets addObject:photoALAssets];
  117. }
  118. }];
  119. }
  120. - (void)imageWithAsset:(PHAsset *)asset targetSize:(CGSize)targetSize completion:(kGWLPhotoSelector_imageBlock)completion {
  121. [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:targetSize contentMode:PHImageContentModeDefault options:self.imageOptions resultHandler:^(UIImage *result, NSDictionary *info) {
  122. completion(result);
  123. }];
  124. }
  125. #pragma mark - getter && setter
  126. - (GWLPhotoGroupTableViewController *)photoGroupTableViewController {
  127. if (!_photoGroupTableViewController) {
  128. _photoGroupTableViewController = [[GWLPhotoGroupTableViewController alloc]init];
  129. }
  130. return _photoGroupTableViewController;
  131. }
  132. - (ALAssetsLibrary *)library {
  133. if (!_library) {
  134. _library = [[ALAssetsLibrary alloc] init];
  135. }
  136. return _library;
  137. }
  138. - (NSMutableArray *)photoGroupArray {
  139. if (!_photoGroupArray) {
  140. _photoGroupArray = [NSMutableArray array];
  141. }
  142. return _photoGroupArray;
  143. }
  144. - (PHImageRequestOptions *)imageOptions {
  145. if (!_imageOptions) {
  146. _imageOptions = [[PHImageRequestOptions alloc] init];
  147. _imageOptions.synchronous = YES;
  148. }
  149. return _imageOptions;
  150. }
  151. - (void)setMaxCount:(NSInteger)maxCount {
  152. _maxCount = maxCount;
  153. self.photoGroupTableViewController.maxCount = maxCount;
  154. self.photoGroupTableViewController.block = self.block;
  155. }
  156. - (void)setMultiAlbumSelect:(BOOL)multiAlbumSelect {
  157. _multiAlbumSelect = multiAlbumSelect;
  158. self.photoGroupTableViewController.multiAlbumSelect = multiAlbumSelect;
  159. }
  160. @end
  161. @implementation GWLPhotoGroup
  162. - (NSMutableArray *)photoALAssets {
  163. if (!_photoALAssets) {
  164. _photoALAssets = [NSMutableArray array];
  165. }
  166. return _photoALAssets;
  167. }
  168. - (void)setGroupName:(NSString *)groupName {
  169. if ([groupName isEqualToString:@"Camera Roll"]) {
  170. groupName = @"相机胶卷";
  171. }
  172. _groupName = groupName;
  173. }
  174. @end
  175. @implementation GWLPhotoALAssets
  176. @end