iOS --- 使用ALAssetsLibrary訪問裝置中的所有照片資訊

來源:互聯網
上載者:User

標籤:ios

ALAssetsLibrary 提供了訪問iOS裝置下照片應用下所有照片和視頻的介面.

從 ALAssetsLibrary 中可讀取所有的相簿資料,即 ALAssetsGroup 對象列表;
從每個 ALAssetsGroup 中可擷取到其中包含的照片或視頻列表,即 ALAsset 對象列表.
其層次關係為 ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation.
1. 每個 ALAsset 可能有多個representations表示, 即ALAssetRepresentation 對象:
2. 使用其defaultRepresentation 方法可獲得其預設representations,
3. 使用[asset valueForProperty: ALAssetPropertyRepresentations ]可擷取其所有representations的 UTI 數組。
4. 從ALAsset對象可擷取縮圖 thumbnail 或 aspectRatioThumbnail ;
5. 從 ALAssetRepresentation 對象可擷取全尺寸圖片(fullResolutionImage), 全屏圖片(fullScreenImage)及圖片的各種屬性: orientation, dimensions, scale, url, metadata等。

如下代碼及其相關注釋:

#import <AssetsLibrary/AssetsLibrary.h>UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 64, self.view.frame.size.width - 20, self.view.frame.size.height - 128)];[self.view addSubview:imageView];ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];// asynchronous// 遍曆assetsgroup[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {    if (group) {        NSLog(@"group : %@", group);        // 封面圖片        UIImage *cover = [[UIImage alloc] initWithCGImage:[group posterImage]];         // imageView.image = cover;        // 同步方法, 只有所有Assets遍曆完才返回. 所以防止阻塞UI線程.        // 遍曆Assets Group中的Assets        // [group setAssetsFilter:[ALAssetsFilter allPhotos]];        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {            if (asset) {                NSLog(@"\nasset : %@", asset);                NSLog(@"ALAssetPropertyAssetURL : %@", [asset valueForProperty:ALAssetPropertyAssetURL]);                UIImage *image = [[UIImage alloc] initWithCGImage:[[asset defaultRepresentation] fullScreenImage]];                // url                NSString *url = [[[asset defaultRepresentation] url] absoluteString];                 NSLog(@"url : %@", url);                // 縮圖                UIImage *thumbnail = [[UIImage alloc] initWithCGImage:[asset thumbnail]];                UIImage *aspectRatioThumbnail = [[UIImage alloc] initWithCGImage:[asset aspectRatioThumbnail]];                // 每個ALAsset都可能有多個representation表示, 即ALAssetRepresentation對象.                // 擷取所有representations的UTI數組                NSArray *utiArrays = [NSArray arrayWithObject:[asset valueForProperty:ALAssetPropertyRepresentations]];                NSLog(@"utiArrays : %@", utiArrays);                // 全尺寸圖                UIImage *fullResolutionImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];                // 全屏圖                UIImage *fullScreenImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];                imageView.image = fullResolutionImage;                // 建立時間                NSString *createTime = (NSString *)[asset valueForProperty:ALAssetPropertyDate];                NSLog(@"createTime : %@", createTime);                // 拍攝位置                NSString *createLocation = (NSString *)[asset valueForProperty:ALAssetPropertyLocation];                NSLog(@"createLocation : %@", createLocation);                // 尺寸                CGSize dimensions = [[asset defaultRepresentation] dimensions];                NSLog(@"dimensions : %f - %f", dimensions.width, dimensions.height);            }        }];    }} failureBlock:^(NSError *error) {    NSLog(@"%@", error);}];

需要注意的是:
1. 通過ALAssetsLibrary對象擷取的其他對象只在該ALAssetsLibrary對象生命期內有效, 即若該ALAssetsLibrary對象被銷毀, 則其他從中擷取的對象都不能再訪問了. 因此, 採用以上block的方式會比較方便.
2. 所有得到的都為CGImageRef對象.
3. 推薦使用fullScreenImage, 因其已調整過. 而fullResolutionImage尺寸太大, 需要自己調整和縮放.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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.