GitHub地址:https://github.com/zhuxinleibandou/ZXLChoosePhoto
Photos框架是iOS新引入进来的,具体的介绍可以百度一下,今天这里主要是写一个照片封装库的心得
上干货~~
引用:http://www.jianshu.com/p/42e5d2f75452
照片库中有两种资源可供获取:PHAsset和PHCollection,前者代表图像或视频对象,后者是前者的集合或自身类型的集合。PHCollection是个基类,有PHAssetCollection和PHCollectionList两个子类,分别代表 Photos 里的相册和文件夹
1.获取相册集
- (PHFetchResult *)getAllCollection{
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
return smartAlbums;
}
2.获取相册内所有照片资源
- (NSMutableArray<PHAsset *> *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending withCollection:(PHCollection *)collection
{
PHFetchOptions *option = [[PHFetchOptions alloc] init];
//ascending 为YES时,按照照片的创建时间升序排列;为NO时,则降序排列
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
option.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary; //设置数据源为本地用户的
NSMutableArray<PHAsset *> *assets = [NSMutableArray array];
BDLog(@"%@",collection.localizedTitle);
if ([collection isKindOfClass:[PHAssetCollection class]]) {
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
if (!asset.hidden) { //是否被隐藏
NSLog(@"照片名%@", [asset valueForKey:@"filename"]);
[assets addObject:asset];
}
}];
}
return assets;
}
3.解析照片
- (void)accessToImageAccordingToTheAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void(^)(UIImage *image,NSDictionary *info))completion
{
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
//仅显示缩略图,不控制质量显示
/**
PHImageRequestOptionsResizeModeNone,
PHImageRequestOptionsResizeModeFast, //根据传入的size,迅速加载大小相匹配(略大于或略小于)的图像
PHImageRequestOptionsResizeModeExact //精确的加载与传入size相匹配的图像
*/
option.resizeMode = resizeMode;
option.networkAccessAllowed = false; //不允许网络请求,icold图片请求不到
//param:targetSize 即你想要的图片尺寸,若想要原尺寸则可输入PHImageManagerMaximumSize
//解析缩略图
[[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
//解析出来的图片
completion(image,info);
}];
}