ios ALAssetsLibrary簡單的使用

來源:互聯網
上載者:User

標籤:

關於ALAssetsLibrary的簡單使用有兩個方面:

第一:儲存圖片/視頻方法如下:

// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;
// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock //- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock

簡單說明:

orientation為儲存圖片時的具體操作:

typedef NS_ENUM(NSInteger, ALAssetOrientation) {
    ALAssetOrientationUp                 // default orientation 預設方向
    ALAssetOrientationDown             // 180 deg rotation
    ALAssetOrientationLeft              // 90 deg CCW
    ALAssetOrientationRight               // 90 deg CW
    ALAssetOrientationUpMirrored          // as above but image mirrored along other axis. horizontal flip 鏡像
    ALAssetOrientationDownMirrored      // horizontal flip
    ALAssetOrientationLeftMirrored        // vertical flip
    ALAssetOrientationRightMirrored      // vertical flip
}

metadata為中繼資料內容,如果內容和圖片內部衝突,將覆蓋操作;

第二:擷取圖片/視頻

以下方法中是非同步擷取相簿內容:

// Get the list of groups that match the given types. Multiple types can be ORed together. The results are passed one by one to the caller by executing the enumeration block.// When the enumeration is done, ‘enumerationBlock‘ will be called with group set to nil.// When groups are enumerated, the user may be asked to confirm the application‘s access to the data. If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.// If the data is currently unavailable, the failure block will be called.- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock 

ALAssetsGroupType參數解析如下:

    ALAssetsGroupLibrary             // The Library group that includes all assets. 所有Asset類型
    ALAssetsGroupAlbum              // All the albums synced from iTunes or created on the device. 所有的專輯同步從iTunes或建立裝置
    ALAssetsGroupEvent               // All the events synced from iTunes. 從iTunes同步
    ALAssetsGroupFaces               // All the faces albums synced from iTunes. iTunes專輯同步
    ALAssetsGroupSavedPhotos     // The Saved Photos album.   儲存圖片
#if __IPHONE_5_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    ALAssetsGroupPhotoStream     // The PhotoStream album.
#endif
    ALAssetsGroupAll                    // The same as ORing together all the available group types,所有組別

獲得組別後 對組別進行操作:

//組封面
- (CGImageRef)posterImage
//組別個數
- (NSInteger)numberOfAssets
//約束
- (void)setAssetsFilter:(ALAssetsFilter *)filter
//枚舉
- (void)enumerateAssetsUsingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock - (void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock - (void)enumerateAssetsAtIndexes:(NSIndexSet *)indexSet options:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock

參數說明:

ALAssetsFilter:

+ (ALAssetsFilter *)allPhotos;
+ (ALAssetsFilter *)allVideos;
+ (ALAssetsFilter *)allAssets;

NSEnumerationOptions:正反排序

NSIndexSet:組中第N個

對於ALAsset的說明:

方法:

  - (CGImageRef)thumbnail

  - (CGImageRef)aspectRatioThumbnail

  - (ALAssetRepresentation *)defaultRepresentation

  Representation屬性:

  – CGImageWithOptions:

  – fullResolutionImage(完全解析度的圖片)

  – fullScreenImage(滿屏的圖片

  - (id)valueForProperty:(NSString *)property

property:

    1.ALAssetPropertyType 資源的類型(照片,視頻)

      2.ALAssetPropertyLocation 資源地理位置(無位置資訊返回null)

      3.ALAssetPropertyDuation 播放時間長度(照片返回ALErorInvalidProperty)

      4.ALAssetPropertyOrientation 方向(共有8個方向,參見:ALAssetOrientation)

      5.ALAssetPropertyDate 拍攝時間(包含了年與日時分秒)

      6.ALAssetPropertyRepresentations 描述(列印看了下,只有帶尾碼的名稱)

      7.ALAssetPropertyURLs(返回一個字典,索引值分別是檔案名稱和檔案的url)

      8.ALAssetPropertyAssetURL 檔案的url )

    editable  property(指示資源是否可以編輯,唯讀屬性)

    originalAsset  property(原始資源。若沒有儲存修改後資源,則原始資源為nil)

My Code:

 

-(void)getAllPicturesAndVideo{    //失敗控制    ALAssetsLibraryAccessFailureBlock failureblock =    ^(NSError *myerror)    {        NSLog(@"相簿訪問失敗 =%@", [myerror localizedDescription]);        if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {            NSLog(@"無法訪問相簿.請在‘設定->定位服務‘設定為開啟狀態.");        }else{            NSLog(@"相簿訪問失敗.");        }        return ;    };    mutableArray =[[NSMutableArray alloc]init];
  //成功 ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration =
  ^(ALAssetsGroup* group,BOOL* stop){
   //說明枚舉結束用group==nil標識 if (group!=nil) { [mutableArray addObject:group]; } else { NSLog(@"%ld",mutableArray.count);
         //更新UI } }; [[ViewController defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:libraryGroupsEnumeration failureBlock:failureblock]; }

 

//從組別中擷取第index個資料
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { //圖片 if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { UIImage *image=[UIImage imageWithCGImage:[result aspectRatioThumbnail]];
         }
     //視頻
     else if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]){ UIImage *image=[UIImage imageWithCGImage:[result aspectRatioThumbnail]]; } }];

 

 

 

 

ios ALAssetsLibrary簡單的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.