AssetsLibrary框架是專門用來操做相冊相關資源的一個框架,其是iOS4到iOS9之間常使用的一個框架,在iOS9以後,系統系統了Photos框架代替了AssetsLibrary框架,可是AssetsLibrary框架依然可使用,而且其結構和設計思路依然值得咱們進行分析學習。框架
AssetsLibrary框架會操做系統的相冊,所以首先須要進行權限的申請,在使用以前,首先須要在Info.plist文件中添加以下鍵值:ide
Privacy - Photo Library Usage Description函數
AssetsLibrary框架中核心的類關係以下圖所示:post
ALAssetsLibrary類用來構建資源庫對象,這個對象用來總體操做系統的相冊資源,在使用它以前咱們可使用下面的方法來獲取用戶的受權狀況:學習
+ (ALAuthorizationStatus)authorizationStatus;
ALAuthorizationStatus枚舉定義了用戶的受權狀況,定義以下:ui
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) { ALAuthorizationStatusNotDetermined, // 用戶還沒有選擇是否受權 ALAuthorizationStatusRestricted, //應用還沒有受權 ALAuthorizationStatusDenied), // 用戶拒絕受權 ALAuthorizationStatusAuthorized // 用戶已經受權 }
若是用戶還沒有受權過,那麼任何訪問操做都將觸發受權機制。編碼
資源庫中的資源數據是以組的方式進行存儲,下面代碼示例了獲取資源組的方式:atom
_library = [[ALAssetsLibrary alloc]init]; [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (group) { // 遍歷相冊還未結束 // 設置過濾器 [group setAssetsFilter:[ALAssetsFilter allPhotos]]; if (group.numberOfAssets) { NSLog(@"%@",group); } } else { // 遍歷結束(當group爲空的時候就意味着結束) NSLog(@"沒有相冊列表了"); } } failureBlock:^(NSError *error) { NSLog(@"失敗"); }];
上面示例的枚舉函數用來根據參數類型獲取資源組,ALAssetsGroupType參數決定獲取組的類型,可選值枚舉以下:url
enum { ALAssetsGroupLibrary ,// 編輯庫 ALAssetsGroupAlbum ,//相冊庫 ALAssetsGroupEvent ,//事件庫 ALAssetsGroupFaces ,// iTunes同步 ALAssetsGroupSavedPhotos ,// 保存的相片 ALAssetsGroupPhotoStream ,// The PhotoStream album. ALAssetsGroupAll ,//全部庫 };
枚舉過程當中,咱們能夠過去到ALAssetsGroup類型的對象,這個對象中封裝了相片資源信息,後面會介紹。spa
下面列舉了ALAssetsLibrary中其餘經常使用的方法:
//直接經過URL來獲取資源 - (void)assetForURL:(NSURL *)assetURL resultBlock:(ALAssetsLibraryAssetForURLResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock; //直接經過URL來獲取資源組 - (void)groupForURL:(NSURL *)groupURL resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock; //向相冊庫中添加一個新的資源組 能夠自定義名稱 - (void)addAssetsGroupAlbumWithName:(NSString *)name resultBlock:(ALAssetsLibraryGroupResultBlock)resultBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock; //向相冊中寫入一張圖片 orientation參數設置圖片的方向 /* typedef NS_ENUM(NSInteger, ALAssetOrientation) { ALAssetOrientationUp , // 向上 默認的 ALAssetOrientationDown , // 向下 ALAssetOrientationLeft , // 向左 ALAssetOrientationRight , // 向右 ALAssetOrientationUpMirrored , // ALAssetOrientationDownMirrored , // horizontal flip ALAssetOrientationLeftMirrored , // vertical flip ALAssetOrientationRightMirrored , // vertical flip }; */ - (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;
當資源庫改變時,系統會發出以下通知:
//資源庫改變的通知 extern NSString *const ALAssetsLibraryChangedNotification;
通知中傳遞的信息中包含以下字段:
//資源庫更新 extern NSString *const ALAssetLibraryUpdatedAssetsKey; //插入組 extern NSString *const ALAssetLibraryInsertedAssetGroupsKey; //更新組 extern NSString *const ALAssetLibraryUpdatedAssetGroupsKey; //刪除組 extern NSString *const ALAssetLibraryDeletedAssetGroupsKey;
下面列舉了操做過程當中的一些異常定義:
enum { ALAssetsLibraryUnknownError = -1, // 未知錯誤 ALAssetsLibraryWriteFailedError = -3300, //寫入錯誤 ALAssetsLibraryWriteBusyError = -3301, // 寫入繁忙 能夠重試 ALAssetsLibraryWriteInvalidDataError = -3302, // 無效數據 ALAssetsLibraryWriteIncompatibleDataError = -3303, // 不兼容的數據 ALAssetsLibraryWriteDataEncodingError = -3304, // 數據編碼錯誤 ALAssetsLibraryWriteDiskSpaceError = -3305, // 內存不足 ALAssetsLibraryDataUnavailableError = -3310, // 數據不可用 ALAssetsLibraryAccessUserDeniedError = -3311, // 權限錯誤 ALAssetsLibraryAccessGloballyDeniedError = -3312, // 權限錯誤 };
資源組其實就是對應與咱們相冊中的一組資源,咱們能夠經過以下的方便遍歷出其中的全部資源:
_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是封裝好的資源對象類,以下方法能夠獲取到資源中封裝的屬性:
- (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;
每個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;