有專案內容是要抓到照片裡的照片資訊,因此做出一些整理.
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
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
NSLog(@"group : %@", group);
// ㄊ
UIImage *cover = [[UIImage alloc] initWithCGImage:[group posterImage]];
[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對象.
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);
}];
沒有留言:
張貼留言