IOS图片操作
PhotoManager.h
@interface PhotoMeanager : NSObject
/**获取单列对象 */
+(instancetype)sharedManager;
/**读取所有相册的信息,返回数组<albumModel *> */
-(void)loadAlbumInfoWithConpletionBlock:(void(^)(NSArray *albumModelArray)) completionBlock;
/**读取某个asset的图片 */
-(void)getThumbnailWithAsset:(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
@property (nonatomic,strong) PHCachingImageManager *cacheImageManager;
-(void)loadAlbumInfoWithConpletionBlock:(void (^)(NSArray *))completionBlock{
NSMutableArray *albumModelsArray = [NSMutableArray array];//存放相册信息
//创建读取那些相册的subType
PHAssetCollectionSubtype subType = PHAssetCollectionSubtypeSmartAlbumVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary|PHAssetCollectionSubtypeSmartAlbumScreenshots;
//获取所有相册的信息PHFetchResult《PHAssetCollection *》
PHFetchResult *albumsCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:subType options:nil];
//2遍历数组albumCollection 获取每一个相册的具体信息
[albumsCollection enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//将obj转化为某个具体类型 PHAseetCollection 代表一个相册
PHAssetCollection *collection = (PHAssetCollection *)obj;
//创建读取相册信息的options
PHFetchOptions *option = [[PHFetchOptions alloc]init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
//读取相册所有信息 PHAssetResult<PHAsset *>
PHFetchResult *assetsResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
//创建一个model,封装相册的信息
AlbumModel *model = [[AlbumModel alloc]init];
model.albumName = collection.localizedTitle;//相册名
model.count = assetsResult.count;
model.result = assetsResult;//保存相册的信息内容
[albumModelsArray addObject:model];
}];
//回调
// completionBlock ? completionBlock(albumModelsArray):nil;
if (completionBlock != nil) {
completionBlock(albumModelsArray);
}
}
-(PHCachingImageManager *)cacheImageManager{
if (_cacheImageManager == nil) {
self.cacheImageManager = [[PHCachingImageManager alloc]init];
}
return _cacheImageManager;
}
-(void)getThumbnailWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//创建异步读取图片的选项
PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = YES;
CGFloat scale = [UIScreen mainScreen].scale;
[self.cacheImageManager requestImageForAsset:asset targetSize:CGSizeMake(80*scale, 80*scale) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回调读取的图片
completionBlock ? completionBlock(result):nil;
}];
}
-(void)getPreVIewImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//创建异步读取图片的选项
PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = YES;
CGFloat scale = [UIScreen mainScreen].scale;
CGSize screenSize = [UIScreen mainScreen].bounds.size;
[self.cacheImageManager requestImageForAsset:asset targetSize:CGSizeMake(scale*screenSize.width, screenSize.height*scale) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回调读取的图片
completionBlock ? completionBlock(result):nil;
}];
}
-(void)getOriginImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
//创建异步读取图片的选项
PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.resizeMode = PHImageRequestOptionsResizeModeExact;
options.synchronous = YES;
[self.cacheImageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//回调读取的图片
completionBlock ? completionBlock(result):nil;
}];
}
viewDidload.m
- (void)viewDidLoad {
[super viewDidLoad];
//首先查看是否授权
if ([PHPhotoLibrary authorizationStatus]!= PHAuthorizationStatusAuthorized) {
NSLog(@"mei");
}
//PHAsset 可以表示一个图片的信息,相册信息
//PHFetchResult 就是一个数组,当读取相册信息,那么这个数组保存的是PHAssetCollection对象,一个PHAssetCollection对象表示一个相册,我的照片流,它里面包含了相册的名字,如果需要读取这个相册里面的所有图片信息,使用PHFetchResult,从PHAssetCollertion里面解析此刻的PHFetchResult数组存放的是这个相册的每一个图片的每一个图片的信息PHAsset;
//读取所有相册的信息
PHFetchResult *collectonResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
//遍历整个相册,对某一个具体相册进行操作
[collectonResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//这个相册的信息
PHAssetCollection *assetCollection = (PHAssetCollection *)obj;
//按照创建的先后顺序查找
PHFetchOptions *option = [[PHFetchOptions alloc]init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationData" ascending:YES]];
//获取相册信息
PHFetchResult *albumResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
//获取相册名字
NSString *albumName = assetCollection.localizedTitle;
//这个 相册的相片个数
NSInteger count = albumResult.count;
NSLog(@"%@ (%ld)",albumName,count);
//读取相册里面的最近一张图片信息PHAsset
PHAsset *lastAsset = [albumResult lastObject];
/*targetSize:需要读取的图片大小
contentMode:图片以什么形式拉伸AspectFit
resizeMode:
excat:高清图片 当选择原图,targetSize必须是原图大小
fast:以最快的速度读取,像素低
none:不重新设置大小
*/
//读取图片的选项
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc]init];
imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone;
__weak ViewController *weakSelf = self;
[[PHImageManager defaultManager]requestImageForAsset:lastAsset targetSize:CGSizeMake(80, 80) contentMode:PHImageContentModeAspectFit options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
weakSelf.imageView.image = result;
}];
}];
}
@end