四、ALAssetsGroup资源组对象
资源组其实就是对应与我们相册中的一组资源,我们可以通过如下的方便遍历出其中的所有资源:
_library = [[ALAssetsLibrary alloc]init];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) { // 遍历相册还未结束
// 设置过滤器
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group.numberOfAssets) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
NSLog(@"%d:%@",index,result);
}];
}
} else { // 遍历结束(当group为空的时候就意味着结束)
NSLog(@"没有相册列表了");
}
} failureBlock:^(NSError *error) {
NSLog(@"失败");
}];
ALAssetsGroup中相关方法解析如下:
//获取相关属性
/*
extern NSString *const ALAssetsGroupPropertyName;//组名字
extern NSString *const ALAssetsGroupPropertyType;//组类型
extern NSString *const ALAssetsGroupPropertyPersistentID; //ID
extern NSString *const ALAssetsGroupPropertyURL;//组URL
*/
- (id)valueForProperty:(NSString *)property;
//获取当前组的缩略图海报
- (CGImageRef)posterImage;
//设置过滤器
- (void)setAssetsFilter:(ALAssetsFilter *)filter;
//获取组中资源个数
- (NSInteger)numberOfAssets;
//进行资源枚举
- (void)enumerateAssetsUsingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
/*
typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) {
NSEnumerationConcurrent = (1UL << 0),//顺序枚举
NSEnumerationReverse = (1UL << 1), //逆序枚举
};
*/
- (void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
- (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
//获取当前组是否允许编辑
@property (nonatomic, readonly, getter=isEditable) BOOL editable;
//向组中添加一个资源
- (BOOL)addAsset:(ALAsset *)asset;
上面有提到资源过滤器,资源过滤器用来设置过滤组中的资源,有3个类方法可以直接获取系统提供的过滤器:
@interface ALAssetsFilter : NSObject {
//所有图片资源
+ (ALAssetsFilter *)allPhotos;
// 所有视频资源
+ (ALAssetsFilter *)allVideos;
// 所有资源
+ (ALAssetsFilter *)allAssets;
@end
五、ALAsset资源对象
ALAsset是封装好的资源对象类,如下方法可以获取到资源中封装的属性:
- (id)valueForProperty:(NSString *)property;
属性名的定义如下:
//获取资源类型
/*
这个属性将返回一个字符串
extern NSString *const ALAssetTypePhoto//照片类型
extern NSString *const ALAssetTypeVideo//视频类型
extern NSString *const ALAssetTypeUnknown//未知类型
*/
extern NSString *const ALAssetPropertyType;
//会返回一个CLLocation对象 图片的地址信息
extern NSString *const ALAssetPropertyLocation;
//视频资源的时长 NSNumber对象
extern NSString *const ALAssetPropertyDuration;
//资源方向
extern NSString *const ALAssetPropertyOrientation;
//资源日期 会返回NSDate对象
extern NSString *const ALAssetPropertyDate;
下面列举了ALAsset中常用方法:
//获取默认的Representation对象
- (ALAssetRepresentation *)defaultRepresentation;
//获取指定的Representation对象
- (ALAssetRepresentation *)representationForUTI:(NSString *)representationUTI;
//获取资源缩略图
- (CGImageRef)thumbnail;
- (CGImageRef)aspectRatioThumbnail;
//写入图片数据
- (void)writeModifiedImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
//写入视频数据
- (void)writeModifiedVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;
//原始资源对象
@property (nonatomic, readonly) ALAsset *originalAsset;
//是否允许编辑
@property (nonatomic, readonly, getter=isEditable) BOOL editable;
//替换图片数据
- (void)setImageData:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
//替换视频数据
- (void)setVideoAtPath:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;
六、关于ALAssetRepresentation类
每一个ALAsset对象中都封装了一个ALAssetRepresentation对象,这个对象的作用是获取资源的详细信息,解析如下:
//获取UTI
- (NSString *)UTI;
//获取资源的尺寸
- (CGSize)dimensions;
//获取资源的大小
- (long long)size;
//读取数据
- (NSUInteger)getBytes:(uint8_t *)buffer fromOffset:(long long)offset length:(NSUInteger)length error:(NSError **)error;
//获取图片数据
- (CGImageRef)fullResolutionImage;
- (CGImageRef)CGImageWithOptions:(NSDictionary *)options;
//获取全屏图片
- (CGImageRef)fullScreenImage;
//获取资源URL
- (NSURL *)url;
//获取资源元数据
- (NSDictionary *)metadata;
//获取资源方向
- (ALAssetOrientation)orientation;
//缩放比
- (float)scale;
//获取资源名称
- (NSString *)filename;