[_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[_assetsArray addObject:group];
[_tableView reloadData];
}
} failureBlock:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:@"相簿取得失敗"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:nil];
[alert show];
}];
[_assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
- (void)getImages {
// 依每列照片數計算縮圖邊長
_thumbnailMeasure = (SCREEN_WIDTH - (self.numberOfImageEachRow + 1) * PHOTO_DEFAULT_MARGIN)
/ self.numberOfImageEachRow;
CGRect frame = CGRectMake(0, 0, _thumbnailMeasure, _thumbnailMeasure);
[self.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result,
NSUInteger index,
BOOL *stop) {
if (result) {
AssetView *assetView = [[AssetView alloc] initWithAsset:result
WithFrame:frame];
assetView.delegate = self;
[_assetsArray addObject:assetView];
}
}];
}
取得資源組名稱:
[_assetsGroup valueForProperty:ALAssetsGroupPropertyName];
取得資源組的封面縮圖:
[UIImage imageWithCGImage:_assetsGroup.posterImage];
取得單張照片的正方形縮圖:
[UIImage imageWithCGImage:_asset.thumbnail];
取得照片完整解析度影像:
[[_asset defaultRepresentation] fullResolutionImage];
取得資源類型:
[_asset valueForProperty:ALAssetPropertyType];
Photos 框架取代,並於 iOS 9 起標記為棄用。新專案請直接使用 PHAssetCollection 與 PHFetchResult,API 設計也更清晰。NSPhotoLibraryUsageDescription,否則會直接 crash。fullResolutionImage 會一次載入整張原圖,大量照片時容易造成記憶體警告。建議搭配 requestImageAtLocation 之類的縮圖取用方式,或改用 Photos 框架的 requestImageForAsset:targetSize:。AssetView 的點擊回呼中同步勾選狀態與右上角數字標記。