123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- //
- // JXHAlbumManager.m
- // 我的社保
- //
- // Created by shanp on 2020/9/15.
- //
- #import "JXHPhotoAlbumManager.h"
- #import "JXHMacro.h"
- #import "JXHPhotoManager.h"
- #import "JXHPhotoPullManager.h"
- @implementation JXHPhotoAlbum
- @end
- @implementation JXHPhotoAlbumManager
- - (JXHPhotoAlbumStatus)getPhotosAuthorization {
- PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
- if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
- return JXHPhotoAlbumStatusNoStatus;
- }
- if (authorizationStatus == PHAuthorizationStatusDenied || authorizationStatus == PHAuthorizationStatusRestricted) {
- return JXHPhotoAlbumStatusNoAuthorization;
- }
- return JXHPhotoAlbumStatusNormal;
- }
- - (void)requestAuthorization:(void(^)(JXHPhotoAlbumStatus status))authorization {
- [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
- if (status == PHAuthorizationStatusAuthorized) {
- authorization(JXHPhotoAlbumStatusNormal);
- } else {
- authorization(JXHPhotoAlbumStatusNoAuthorization);
- }
- }];
- }
- - (void)getLibraryWithCompletion:(void(^)(NSArray<JXHPhotoAlbum *> *, JXHPhotoAlbumResult))completion {
- NSMutableArray *albumsArray = NSMutableArray.new;
- NSSet * filter;
- if (_jxh_canChooseVideo) {
- filter = [NSSet setWithObjects:@3, @4, @6, @214, @216, nil];
- } else {
- filter = [NSSet setWithObjects:@3, @4, @6, @214, @216, @202, @208, nil];
- }
-
- PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
- for (PHAssetCollection *assetCollection in smartAlbums) {
- debugLog(@"%@", assetCollection);
- debugLog(@"%ld", (long)assetCollection.assetCollectionSubtype);
- debugLog(@"%lu", (unsigned long)assetCollection.estimatedAssetCount);
- if ([filter containsObject:@(assetCollection.assetCollectionSubtype)]) {
- continue;
- }
- JXHPhotoAlbum *photoAlbum = [self getPhotoAlbumWithAssetCollection:assetCollection];
- if (photoAlbum) {
- if (assetCollection.assetCollectionSubtype == 209) {
- [albumsArray insertObject:photoAlbum atIndex:0];
- } else {
- [albumsArray addObject:photoAlbum];
- }
- }
-
- }
- PHFetchResult *userAlbums = [PHAssetCollection fetchTopLevelUserCollectionsWithOptions:nil];
- [self getAlbumsWithResult:userAlbums array:albumsArray];
-
- if ([albumsArray count] > 0) {
- // 把所有的相册储存完毕,可以展示相册列表
- completion(albumsArray, JXHPhotoAlbumResultSuccess);
- } else {
- // 没有任何有资源的相册,输出提示
- completion(nil, JXHPhotoAlbumResultNoResource);
- }
- }
- - (void)getAlbumsWithResult:(PHFetchResult *)userAlbums array:(NSMutableArray<JXHPhotoAlbum *> *)albumsArray {
- for (PHCollection *assetCollection in userAlbums) {
- if ([assetCollection isKindOfClass:[PHCollectionList class]]) {
- PHFetchResult *listAlbums = [PHCollectionList fetchCollectionsInCollectionList:(PHCollectionList *)assetCollection options:nil];
- [self getAlbumsWithResult:listAlbums array:albumsArray];
- } else if ([assetCollection isKindOfClass:[PHAssetCollection class]]) {
- JXHPhotoAlbum *photoAlbum = [self getPhotoAlbumWithAssetCollection:(PHAssetCollection *)assetCollection];
- if (photoAlbum) {
- [albumsArray addObject:photoAlbum];
- }
- }
-
- }
- }
- - (nullable JXHPhotoAlbum *)getPhotoAlbumWithAssetCollection:(PHAssetCollection *)assetCollection {
- PHFetchOptions *options = PHFetchOptions.new;
- options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:true]];
- PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection options:options];
- debugLog(@"--------%@", fetchResult);
- debugLog(@"--------%ld", (long)fetchResult.count);
- if (fetchResult && fetchResult.count != 0 && [fetchResult isKindOfClass:[PHFetchResult<PHAsset *> class]]) {
- JXHPhotoAlbum *photoAlbum = JXHPhotoAlbum.new;
- photoAlbum.jxh_albumName = assetCollection.localizedTitle;
- photoAlbum.jxh_albumCount = fetchResult.count;
- photoAlbum.jxh_phassetResult = fetchResult;
- JXHPhotoManager *photoManger = JXHPhotoManager.new;
- photoManger.jxh_phAsset = fetchResult[fetchResult.count - 1];
- [photoManger requestThumbnailImageWithSize:CGSizeMake(60, 60) contentMode:JXHImageContentModeAspectFill completion:^(UIImage *image, NSDictionary *dict) {
- photoAlbum.jxh_albumPosterImage = image;
- }];
- return photoAlbum;
- }
- return nil;
- }
- @end
|