iOS开发之AssetsLibrary框架使用
一、引言
AssetsLibrary框架是专门用来操作相册相关资源的一个框架,其是iOS4到iOS9之间常使用的一个框架,在iOS9之后,系统系统了Photos框架代替了AssetsLibrary框架,但是AssetsLibrary框架依然可以使用,并且其结构和设计思路依然值得我们进行分析学习。
二、概述
AssetsLibrary框架会操作系统的相册,因此首先需要进行权限的申请,在使用之前,首先需要在Info.plist文件中添加如下键值:
Privacy - Photo Library Usage Description
AssetsLibrary框架中核心的类关系如下图所示:
三、ALAssetsLibrary资源库对象
ALAssetsLibrary类用来构建资源库对象,这个对象用来整体操作系统的相册资源,在使用它之前我们可以使用下面的方法来获取用户的授权情况:
1
| + (ALAuthorizationStatus)authorizationStatus;
|
ALAuthorizationStatus枚举定义了用户的授权情况,定义如下:
1 2 3 4 5 6
| typedef NS_ENUM(NSInteger, ALAuthorizationStatus) { ALAuthorizationStatusNotDetermined, ALAuthorizationStatusRestricted, ALAuthorizationStatusDenied), ALAuthorizationStatusAuthorized }
|
如果用户尚未授权过,那么任何访问操作都将触发授权机制。
资源库中的资源数据是以组的方式进行存储,下面代码示例了获取资源组的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| _library = [[ALAssetsLibrary alloc]init]; [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (group) { [group setAssetsFilter:[ALAssetsFilter allPhotos]]; if (group.numberOfAssets) { NSLog(@"%@",group); } } else { NSLog(@"没有相册列表了"); } } failureBlock:^(NSError *error) { NSLog(@"失败"); }];
|
上面示例的枚举函数用来根据参数类型获取资源组,ALAssetsGroupType参数决定获取组的类型,可选值枚举如下:
1 2 3 4 5 6 7 8 9
| enum { ALAssetsGroupLibrary , ALAssetsGroupAlbum , ALAssetsGroupEvent , ALAssetsGroupFaces , ALAssetsGroupSavedPhotos , ALAssetsGroupPhotoStream , ALAssetsGroupAll , };
|
枚举过程中,我们可以过去到ALAssetsGroup类型的对象,这个对象中封装了相片资源信息,后面会介绍。
下面列举了ALAssetsLibrary中其他常用的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| - (void)assetForURL:(NSURL *)assetURL resultBlock:(ALAssetsLibraryAssetForURLResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;
- (void)groupForURL:(NSURL *)groupURL resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;
- (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock;
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;
- (BOOL)videoAtPathIsCompatibleWithSavedPhotosAlbum:(NSURL *)videoPathURL;
|
当资源库改变时,系统会发出如下通知:
1 2
| extern NSString *const ALAssetsLibraryChangedNotification;
|
通知中传递的信息中包含如下字段:
1 2 3 4 5 6 7 8
| extern NSString *const ALAssetLibraryUpdatedAssetsKey;
extern NSString *const ALAssetLibraryInsertedAssetGroupsKey;
extern NSString *const ALAssetLibraryUpdatedAssetGroupsKey;
extern NSString *const ALAssetLibraryDeletedAssetGroupsKey;
|
下面列举了操作过程中的一些异常定义:
1 2 3 4 5 6 7 8 9 10 11 12
| enum { ALAssetsLibraryUnknownError = -1, ALAssetsLibraryWriteFailedError = -3300, ALAssetsLibraryWriteBusyError = -3301, ALAssetsLibraryWriteInvalidDataError = -3302, ALAssetsLibraryWriteIncompatibleDataError = -3303, ALAssetsLibraryWriteDataEncodingError = -3304, ALAssetsLibraryWriteDiskSpaceError = -3305, ALAssetsLibraryDataUnavailableError = -3310, ALAssetsLibraryAccessUserDeniedError = -3311, ALAssetsLibraryAccessGloballyDeniedError = -3312, };
|
四、ALAssetsGroup资源组对象
资源组其实就是对应与我们相册中的一组资源,我们可以通过如下的方便遍历出其中的所有资源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| _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 {
NSLog(@"没有相册列表了"); }
} failureBlock:^(NSError *error) { NSLog(@"失败"); }];
|
ALAssetsGroup中相关方法解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
- (id)valueForProperty:(NSString *)property;
- (CGImageRef)posterImage;
- (void)setAssetsFilter:(ALAssetsFilter *)filter;
- (NSInteger)numberOfAssets;
- (void)enumerateAssetsUsingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;
- (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个类方法可以直接获取系统提供的过滤器:
1 2 3 4 5 6 7 8
| @interface ALAssetsFilter : NSObject {
+ (ALAssetsFilter *)allPhotos;
+ (ALAssetsFilter *)allVideos;
+ (ALAssetsFilter *)allAssets; @end
|
五、ALAsset资源对象
ALAsset是封装好的资源对象类,如下方法可以获取到资源中封装的属性:
1
| - (id)valueForProperty:(NSString *)property;
|
属性名的定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
extern NSString *const ALAssetPropertyType;
extern NSString *const ALAssetPropertyLocation;
extern NSString *const ALAssetPropertyDuration;
extern NSString *const ALAssetPropertyOrientation;
extern NSString *const ALAssetPropertyDate;
|
下面列举了ALAsset中常用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| - (ALAssetRepresentation *)defaultRepresentation;
- (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对象,这个对象的作用是获取资源的详细信息,解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| - (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;
- (NSURL *)url;
- (NSDictionary *)metadata;
- (ALAssetOrientation)orientation;
- (float)scale;
- (NSString *)filename;
|