Photos框架实现自定义从系统相册中选取

作者: Mustard_Buli | 来源:发表于2016-03-22 11:49 被阅读1402次

Photos 框架实践以及坑

PhotoManager.h

#import <Foundation/Foundation.h>
#import <Photos/Photos.h>

@interface PhotoManager : NSObject

/** 获取单例对象 PhotoManager */
+ (instancetype)sharedManager;

/** 读取所有相册的信息 返回数组<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];
    
    //创建读取哪些相册的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;
        
        //创建读取相册信息的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) {
            //创建一个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)getThumbnailImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
    
    //创建异步读取图片的选项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) {
        //回调读取的图片
        completionBlock ? completionBlock(result) : nil;
    }];
}

- (void)getPreviewImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
    //创建异步读取图片的选项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) {
        //回调读取的图片
        completionBlock ? completionBlock(result) : nil;
    }];

}

- (void)getOriginImageWithAsset:(PHAsset *)asset completionBlock:(void (^)(UIImage *))completionBlock{
    //创建异步读取图片的选项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) {
        //回调读取的图片
        completionBlock ? completionBlock(result) : nil;
    }];
}
@end

新写了一个项目,安利一下!还在用UIImagePickerController? ——iOS相册选择器

相关文章

网友评论

  • Chrisss:PHAssetCollectionSubtypeSmartAlbumVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary|PHAssetCollectionSubtypeSmartAlbumScreenshots;

    这里有问题吧, 这个不是 options 不是这么用的吧...
    比如说:
    PHAssetCollectionSubtypeSmartAlbumPanoramas | PHAssetCollectionSubtypeSmartAlbumVideos, 本来是想得到全景和视频,但是这个表达式的结果等于 203, 也就是 PHAssetCollectionSubtypeSmartAlbumFavorites
    建议去看一下 bool 运算
  • 5f48ff8b703c:求源代码,395240625@qq.com,谢谢了!
  • xcode9:能发一下源码吗?1149191294@qq.com
    Mustard_Buli:@xcode9 已经发过去了。
  • LaiYoung_:15923456720@163.com 谢谢
    LaiYoung_:thanks
    Mustard_Buli:@LaiYoung 已经发过去了。
    Mustard_Buli:@LaiYoung 等我回去的。我现在在外面。
  • 张无忌_:很不要脸的照着抄了一份swift版的:sob:
    Mustard_Buli:我还不会swift 呢。。
  • 张无忌_:hello, 能发我一下源码吗?
    Mustard_Buli:@张无忌_ 已经发送了。
    张无忌_:@Mustard_iOS 发这个邮箱745258163@qq.com 谢谢 🐥
    Mustard_Buli:@张无忌_ 怎么给你啊。

本文标题:Photos框架实现自定义从系统相册中选取

本文链接:https://www.haomeiwen.com/subject/dafllttx.html