思路:
导入头文件#importPHAsset :
一个资源, 比如一张图片\一段视频
PHAssetCollection : 一个相簿PHImageManager
图片管理者,是单例,发送请求才能从asset获取图片PHImageRequestOptions图片
请求选项注意:这个类是iOS8开始推广,iOS9开始废弃之前的方法系统适配iOS8之前,用下面这个库里面的 API#import1.获得所有相簿的原图
- (void)getOriginalImages{
// 获得所有的自定义相簿
PHFetchResult*assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; // 遍历所有的自定义相簿
for (PHAssetCollection *assetCollection in assetCollections) { [self enumerateAssetsInAssetCollection:assetCollection original:YES]; } // 获得相机胶卷
PHAssetCollection *cameraRoll = [PHAssetCollection
fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject; // 遍历相机胶卷,获取大图
[self enumerateAssetsInAssetCollection:cameraRoll original:YES];}
2.获得所有相簿中的缩略图
- (void)getThumbnailImages{
// 获得所有的自定义相簿
PHFetchResult*assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 遍历所有的自定义相簿
for (PHAssetCollection *assetCollection in assetCollections) { [self enumerateAssetsInAssetCollection:assetCollection original:NO]; }
// 获得相机胶卷
PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
[self enumerateAssetsInAssetCollection:cameraRoll original:NO];}
3.遍历相册/**
* 遍历相簿中的所有图片
* @param assetCollection 相簿
* @param original 是否要原图
*/
- (void)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original
{
NSLog(@"相簿名:%@", assetCollection.localizedTitle); PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
// 同步获得图片, 只会返回1张图片
options.synchronous = YES;
// 获得某个相簿中的所有PHAsset对象 PHFetchResult*assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
for (PHAsset *asset in assets) {
// 是否要原图
CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero;
// 从asset中获得图片
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(@"%@", result);
}];
}
}