概述
在iOS中如果想要获取手机相册里面的图片或者视频的话就要用到系统自带的AssetsLibrary
框架,AssetsLibrary.framework
中包含以下文件
#import
#import
#import
#import
#import
ALAsset.h
从本地相册获取的图片或者视频的对象都是ALAsset
类型的对象。ALAssetsFilter
顾名思义是对本地所有资源的过滤筛选,可以选择只获取照片,只获取视频,或者获取所有的资源。ALAssetsGroup
本地相册分组对象,本地有多少个相薄就会有几个ALAssetsGroup
类型的对象。ALAssetsLibrary
本地资源库对象ALAssetRepresentation
ALAssetRepresentation
对象来获取视频或者图片的url,文件名,二进制数据,封面缩略图等
AssetsLibrary使用
#import
获取本地所有相薄
初始化ALAssetsLibrary
对象并使用下面的方法来获取本地所有相薄
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock
types
相薄分组类型 ALAssetsGroupType
苹果官方文档定义如下
enum {
ALAssetsGroupLibrary NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 0),
// The Library group that includes all assets.
ALAssetsGroupAlbum NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 1),
// All the albums synced from iTunes or created on the device.
ALAssetsGroupEvent NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 2),
// All the events synced from iTunes.
ALAssetsGroupFaces NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 3),
// All the faces albums synced from iTunes.
ALAssetsGroupSavedPhotos NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = (1 << 4),
// The Saved Photos album.
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
ALAssetsGroupPhotoStream NS_ENUM_DEPRECATED_IOS(5_0, 9_0) = (1 << 5),
// The PhotoStream album.
#endif
ALAssetsGroupAll NS_ENUM_DEPRECATED_IOS(4_0, 9_0) = 0xFFFFFFFF,
// The same as ORing together all the available group types,
};
enumerationBlock
对本地相薄进行遍历并返回遍历结果 ALAssetsLibraryGroupsEnumerationResultsBlock
定义如下
ALAssetsLibraryGroupsEnumerationResultsBlock resultBlock = ^(ALAssetsGroup *group,BOOL *stop) {};
failureBlock
遍历失败 ALAssetsLibraryAccessFailureBlock
定义如下
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {};
代码示例
typedef void (^albumGroupsBlock)(NSMutableArray *groups);
-(void)setupAlbumGroups:(albumGroupsBlock)albumGroups //获取所有相薄成功后回调
{
NSMutableArray *groups = @[].mutableCopy; //所有相薄数组
ALAssetsFilter* assstsFilter = [ALAssetsFilter allAssets]; //筛选器获取所有资源
ALAssetsLibraryGroupsEnumerationResultsBlock resultBlock = ^(ALAssetsGroup *group, BOOL *stop){ //每次遍历成功后回调
if (group) {
[group setAssetsFilter:assstsFilter];
NSInteger groupType = [[group valueForProperty:ALAssetsGroupPropertyType] integerValue];
if (groupType == ALAssetsGroupSavedPhotos) {
[groups insertObject:group atIndex:0]; //默认相机胶卷放入数组第一位
}
else
{
if (group.numberOfAssets>0) {
[groups addObject:group];
}
}
}
else //当返回的group为nil表示已经遍历结束
{
if (albumGroups) {
albumGroups([groups copy]);
}
}
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {
if (albumGroups) { //遍历失败回调
albumGroups([groups copy]);
}
};
;
[[[ALAssetsLibrary alloc] init] enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:resultBlock failureBlock:failureBlock]; //开始遍历
}
获取本地每个相薄中的资源
初始化ALAssetsGroup
对象并使用下面的方法来获取相薄中的资源
- (void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock
options
遍历相薄中资源的顺序 NSEnumerationOptions
定义如下
typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) {
NSEnumerationConcurrent = (1UL << 0), // 并发读取相册中的资源,顺序不确定
NSEnumerationReverse = (1UL << 1), //倒序读取相册中的资源
};
enumerationBlock
对相薄里资源进行遍历并返回遍历结果
ALAssetsGroupEnumerationResultsBlock resultBlock = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {};
代码示例
typedef void (^albumAssetsBlock)(NSMutableArray *assets);
-(void)setupAlbumAssets:(ALAssetsGroup *)group withAssets:(albumAssetsBlock)albumAssets
{
NSMutableArray *assets = @[].mutableCopy; //相薄中所有资源数组
ALAssetsFilter* assstsFilter = [ALAssetsFilter allAssets]; //筛选器获取所有资源
[group setAssetsFilter:assstsFilter]; //相册内资源总数
NSInteger assetCount = [group numberOfAssets]; //相薄中资源总数
ALAssetsGroupEnumerationResultsBlock resultBlock = ^(ALAsset *asset, NSUInteger index, BOOL *stop) { //资源为ALAsset类型对象
if (asset) {
[assets addObject:asset];
NSString *assetType = [asset valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]) {
//资源类型为图片
}
else if ([assetType isEqualToString:ALAssetTypeVideo]) {
//资源类型为视频
}
}
else if (assets.count >= assetCount)
{
if (albumAssets) {
albumAssets([assets copy]);
}
};
};
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:resultBlock];
//开始遍历相薄中资源
}
实现效果
示例代码
https://github.com/GGGHub/AlbumPicker