標籤: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訪問裝置中的所有照片資訊