看到项目中用到AssetsLibrary这个框架, 所以研究了一下
其实很简单
- (void)getAlbumPhoto {
NSString *tipTextWhenNoPhotosAuthorization; // 提示语
// 获取当前应用对照片的访问授权状态
ALAuthorizationStatus authorizationStatus = [ALAssetsLibrary authorizationStatus];
NSLog(@"sss%ld", (long)authorizationStatus);
/*
// 不确定身份
ALAuthorizationStatusNotDetermined NS_ENUM_DEPRECATED_IOS(6_0, 9_0) = 0,
// 受限制授权
ALAuthorizationStatusRestricted NS_ENUM_DEPRECATED_IOS(6_0, 9_0),
// 拒绝
ALAuthorizationStatusDenied NS_ENUM_DEPRECATED_IOS(6_0, 9_0),
// 授权成功
ALAuthorizationStatusAuthorized NS_ENUM_DEPRECATED_IOS(6_0, 9_0)
*/
// 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权
if (authorizationStatus == ALAuthorizationStatusRestricted || authorizationStatus == ALAuthorizationStatusDenied) {
NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleName"];
tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"请在设备的\"设置-隐私-照片\"选项中,允许 %@ 访问你的手机相册", appName];
// 展示提示语
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:tipTextWhenNoPhotosAuthorization delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
[alertView show];
return;
} [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
// 过滤相册列表
/*
// + (ALAssetsFilter *)allPhotos; // 全部的相册
// + (ALAssetsFilter *)allVideos; // 全部的视频
// + (ALAssetsFilter *)allAssets; // 所有视频 + 相册
*/
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group.numberOfAssets > ) {
// 把相册储存到数组中,方便后面展示相册时使用
AssetPhotoAlbum *assetGroupAlbum = [AssetPhotoAlbum assetPhotoAlbumWithAssetsGroup:group];
[self.allAssetGroupAlbum addObject:assetGroupAlbum]; [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
AlbumPhoto *albumPhoto = [AlbumPhoto albumPhotoAsset:result];
[assetGroupAlbum.assetAlbumPhotosArray addObject:albumPhoto];
}
}];
}
}
else {
if (self.allAssetGroupAlbum.count) {
[self.collectionView reloadData];
}
}
} failureBlock:^(NSError *error) {
NSLog(@"Asset group not found!\n");
}];
}