JXHPhotoAlbumManager.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // JXHAlbumManager.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2020/9/15.
  6. //
  7. #import "JXHPhotoAlbumManager.h"
  8. #import "JXHMacro.h"
  9. #import "JXHPhotoManager.h"
  10. #import "JXHPhotoPullManager.h"
  11. @implementation JXHPhotoAlbum
  12. @end
  13. @implementation JXHPhotoAlbumManager
  14. - (JXHPhotoAlbumStatus)getPhotosAuthorization {
  15. PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
  16. if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
  17. return JXHPhotoAlbumStatusNoStatus;
  18. }
  19. if (authorizationStatus == PHAuthorizationStatusDenied || authorizationStatus == PHAuthorizationStatusRestricted) {
  20. return JXHPhotoAlbumStatusNoAuthorization;
  21. }
  22. return JXHPhotoAlbumStatusNormal;
  23. }
  24. - (void)requestAuthorization:(void(^)(JXHPhotoAlbumStatus status))authorization {
  25. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  26. if (status == PHAuthorizationStatusAuthorized) {
  27. authorization(JXHPhotoAlbumStatusNormal);
  28. } else {
  29. authorization(JXHPhotoAlbumStatusNoAuthorization);
  30. }
  31. }];
  32. }
  33. - (void)getLibraryWithCompletion:(void(^)(NSArray<JXHPhotoAlbum *> *, JXHPhotoAlbumResult))completion {
  34. NSMutableArray *albumsArray = NSMutableArray.new;
  35. NSSet * filter;
  36. if (_jxh_canChooseVideo) {
  37. filter = [NSSet setWithObjects:@3, @4, @6, @214, @216, nil];
  38. } else {
  39. filter = [NSSet setWithObjects:@3, @4, @6, @214, @216, @202, @208, nil];
  40. }
  41. PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
  42. for (PHAssetCollection *assetCollection in smartAlbums) {
  43. debugLog(@"%@", assetCollection);
  44. debugLog(@"%ld", (long)assetCollection.assetCollectionSubtype);
  45. debugLog(@"%lu", (unsigned long)assetCollection.estimatedAssetCount);
  46. if ([filter containsObject:@(assetCollection.assetCollectionSubtype)]) {
  47. continue;
  48. }
  49. JXHPhotoAlbum *photoAlbum = [self getPhotoAlbumWithAssetCollection:assetCollection];
  50. if (photoAlbum) {
  51. if (assetCollection.assetCollectionSubtype == 209) {
  52. [albumsArray insertObject:photoAlbum atIndex:0];
  53. } else {
  54. [albumsArray addObject:photoAlbum];
  55. }
  56. }
  57. }
  58. PHFetchResult *userAlbums = [PHAssetCollection fetchTopLevelUserCollectionsWithOptions:nil];
  59. [self getAlbumsWithResult:userAlbums array:albumsArray];
  60. if ([albumsArray count] > 0) {
  61. // 把所有的相册储存完毕,可以展示相册列表
  62. completion(albumsArray, JXHPhotoAlbumResultSuccess);
  63. } else {
  64. // 没有任何有资源的相册,输出提示
  65. completion(nil, JXHPhotoAlbumResultNoResource);
  66. }
  67. }
  68. - (void)getAlbumsWithResult:(PHFetchResult *)userAlbums array:(NSMutableArray<JXHPhotoAlbum *> *)albumsArray {
  69. for (PHCollection *assetCollection in userAlbums) {
  70. if ([assetCollection isKindOfClass:[PHCollectionList class]]) {
  71. PHFetchResult *listAlbums = [PHCollectionList fetchCollectionsInCollectionList:(PHCollectionList *)assetCollection options:nil];
  72. [self getAlbumsWithResult:listAlbums array:albumsArray];
  73. } else if ([assetCollection isKindOfClass:[PHAssetCollection class]]) {
  74. JXHPhotoAlbum *photoAlbum = [self getPhotoAlbumWithAssetCollection:(PHAssetCollection *)assetCollection];
  75. if (photoAlbum) {
  76. [albumsArray addObject:photoAlbum];
  77. }
  78. }
  79. }
  80. }
  81. - (nullable JXHPhotoAlbum *)getPhotoAlbumWithAssetCollection:(PHAssetCollection *)assetCollection {
  82. PHFetchOptions *options = PHFetchOptions.new;
  83. options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:true]];
  84. PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:options];
  85. debugLog(@"--------%@", fetchResult);
  86. debugLog(@"--------%ld", (long)fetchResult.count);
  87. if (fetchResult && fetchResult.count != 0 && [fetchResult isKindOfClass:[PHFetchResult<PHAsset *> class]]) {
  88. JXHPhotoAlbum *photoAlbum = JXHPhotoAlbum.new;
  89. photoAlbum.jxh_albumName = assetCollection.localizedTitle;
  90. photoAlbum.jxh_albumCount = fetchResult.count;
  91. photoAlbum.jxh_phassetResult = fetchResult;
  92. JXHPhotoManager *photoManger = JXHPhotoManager.new;
  93. photoManger.jxh_phAsset = fetchResult[fetchResult.count - 1];
  94. [photoManger requestThumbnailImageWithSize:CGSizeMake(60, 60) contentMode:JXHImageContentModeAspectFill completion:^(UIImage *image, NSDictionary *dict) {
  95. photoAlbum.jxh_albumPosterImage = image;
  96. }];
  97. return photoAlbum;
  98. }
  99. return nil;
  100. }
  101. @end