PhotoManager.h
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface PhotoManager : NSObject
/** 獲取單例對象 PhotoManager */
+ (instancetype)sharedManager;
/** 讀取所有相冊的信息 返回數(shù)組<AlbumModel *> */
- (void)loadAlbumInfoWithCompletionBlock:(void(^)(NSArray * albumsModelArray))completionBlock;
/** 讀取某個asset的圖片*/
- (void)getThumbnailImageWithAsset:(PHAsset *)asset completionBlock:(void(^)(UIImage *thumbnail))completionBlock;
/** 讀取預覽圖片 屏幕的大小*/
- (void)getPreviewImageWithAsset:(PHAsset *)asset completionBlock:(void(^)(UIImage *result))completionBlock;
/** 讀取原圖圖片 */
- (void)getOriginImageWithAsset:(PHAsset *)asset completionBlock:(void(^)(UIImage *result))completionBlock;
@end
PhotoManager.m
#import "PhotoManager.h"
#import "AlbumModel.h"
@interface PhotoManager ()
@property (nonatomic, strong) PHCachingImageManager *cacheImageManager;
@end
@implementation PhotoManager
+ (instancetype)sharedManager{
static PhotoManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[PhotoManager alloc] init];
});
return instance;
}
- (void)loadAlbumInfoWithCompletionBlock:(void (^)(NSArray *))completionBlock{
//用來存放每個相冊model
NSMutableArray *albumModelsArray = [NSMutableArray array];
//創(chuàng)建讀取哪些相冊的subType
PHAssetCollectionSubtype subType = PHAssetCollectionSubtypeSmartAlbumVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary|PHAssetCollectionSubtypeSmartAlbumScreenshots;
//1.獲取所有相冊的信息PHFetchResult<PHAssetCollection *>
PHFetchResult *albumsCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:subType options:nil];
//2.遍歷albumsCollection獲取每一個相冊的具體信息
[albumsCollection enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//將obj轉化為某個具體類型 PHAssetCollection 代表一個相冊
PHAssetCollection *collection = (PHAssetCollection *)obj;
//創(chuàng)建讀取相冊信息的options
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
//讀取相冊里面的所有信息 PHFetchResult <PHAsset *>
PHFetchResult *assetsResult = [PHAsset fetchAssetsInAssetCollection:collection options:options];
if (assetsResult.count > 0) {
//創(chuàng)建一個model封裝這個相冊的信息
AlbumModel *model = [[AlbumModel alloc] init];
model.albumName = collection.localizedTitle;//相冊名
model.count = assetsResult.count; //相冊里面內(nèi)容的個數(shù)(多少圖片或者視頻)
model.result = assetsResult; //保存這個相冊的內(nèi)容
[albumModelsArray addObject:model];
}
}];
//回調(diào)
//completionBlock ? completionBlock(albumModelsArray) : nil;
if (completionBlock != nil) {
completionBlock(albumModelsArray);
}
}
- (PHCachingImageManager *)cacheImageManager{
if (_cacheImageManager == nil) {
self.cacheImageManager = [[PHCachingImageManager alloc] init];
}
return _cacheImageManager;
}
- (void)getThumbnailImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//創(chuàng)建異步讀取圖片的選項options
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = NO;
CGFloat scale = [UIScreen mainScreen].scale;
[self.cacheImageManager requestImageForAsset:asset targetSize:CGSizeMake(70*scale, 70*scale) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回調(diào)讀取的圖片
completionBlock ? completionBlock(result) : nil;
}];
}
- (void)getPreviewImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//創(chuàng)建異步讀取圖片的選項options
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = NO;
CGFloat scale = [UIScreen mainScreen].scale;
CGSize screenSize = [UIScreen mainScreen].bounds.size;
[self.cacheImageManager requestImageForAsset:asset targetSize:CGSizeMake(screenSize.width*scale, screenSize.height*scale) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回調(diào)讀取的圖片
completionBlock ? completionBlock(result) : nil;
}];
}
- (void)getOriginImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//創(chuàng)建異步讀取圖片的選項options
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = NO;
[self.cacheImageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回調(diào)讀取的圖片
completionBlock ? completionBlock(result) : nil;
}];
}
@end