相册框架之AssetsLibrary

Posted Taurin_Coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相册框架之AssetsLibrary相关的知识,希望对你有一定的参考价值。

看到项目中用到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 > 0) {
                // 把相册储存到数组中,方便后面展示相册时使用
                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");
    }];
}

以上是关于相册框架之AssetsLibrary的主要内容,如果未能解决你的问题,请参考以下文章

设备中创建新相册(粘贴)

[iOS开发]iOS调用系统相册相机

相册的选择-单张/多张

iOS相册(多张图片选取)

iOS 开发之照片框架详解

iOS 开发之照片框架详解