iOS AssetsLibrary和Photos的使用总结(1): 权限及相册的获取

本文介绍了一个开源库DLPhotoPicker,该库封装了iOS中AssetsLibrary和Photos框架的API,支持从iOS7到iOS8及更高版本的相册权限检查和相册获取功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码下载:https://github.com/darling0825/DLPhotoPicker.git
1.相册权限获取
AssetsLibrary:

- (void)checkAuthorizationStatus_BeforeiOS8
{
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    switch (status)
    {
        case ALAuthorizationStatusNotDetermined:
        {
            [self requestAuthorizationStatus_AfteriOS8];
            break;
        }
        case ALAuthorizationStatusRestricted:
        case ALAuthorizationStatusDenied:
        {
            [self showAccessDenied];
            break;
        }
        case ALAuthorizationStatusAuthorized:
        default:
        {
            [self checkAuthorizationSuccess];
            break;
        }
    }
}

Photos:

- (void)checkAuthorizationStatus_AfteriOS8
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    switch (status)
    {
        case PHAuthorizationStatusNotDetermined:
        {
            [self requestAuthorizationStatus_AfteriOS8];
            break;
        }
        case PHAuthorizationStatusRestricted:
        case PHAuthorizationStatusDenied:
        {
            [self showAccessDenied];
            break;
        }
        case PHAuthorizationStatusAuthorized:
        default:
        {
            [self checkAuthorizationSuccess];
            break;
        }
    }
}
- (void)requestAuthorizationStatus_AfteriOS8
{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
        dispatch_async(dispatch_get_main_queue(), ^{
            switch (status) {
                case PHAuthorizationStatusAuthorized:
                {
                    [self checkAuthorizationSuccess];
                    break;
                }
                default:
                {
                    [self showAccessDenied];
                    break;
                }
            }
        });
    }];
}

2.获取相册
AssetsLibrary:

- (void)getAlbumsFromDevice_BeforeiOS8
{
    NSMutableArray *albumsArray = [[NSMutableArray alloc] init];
    __weak typeof(self) weakSelf = self;
    [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (group) {
            /**
             *  allPhotos: Get all photos assets in the assets group.
             *  allVideos: Get all video assets in the assets group.
             *  allAssets: Get all assets in the group.
             */
            [group setAssetsFilter:[ALAssetsFilter allAssets]];

            if (!strongSelf.showsEmptyAlbums && group.numberOfAssets <= 0) {
                ;
            }else{
                DLPhotoCollection *collection = [[DLPhotoCollection alloc] initWithAssetCollection:group];
                NSInteger groupType = [[group valueForProperty:ALAssetsGroupPropertyType] integerValue];
                if(groupType == ALAssetsGroupSavedPhotos){
                    [albumsArray insertObject:collection atIndex:0];
                }
                else{
                    [albumsArray addObject:collection];
                }
            }

        } else {
            if ([albumsArray count] > 0) {
                strongSelf.photoCollections = [NSMutableArray arrayWithArray:albumsArray];
                // 把所有的相册储存完毕,可以展示相册列表
                [strongSelf getAlbumsCompletion:YES];
            } else {
                // 没有任何有资源的相册,输出提示
                [strongSelf showNoAssets];
            }
        }
    } failureBlock:^(NSError *error) {
        NSLog(@">>>Asset group not found!\n");
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf getAlbumsCompletion:NO];
    }];
}

Photos

- (void)getAlbumsFromDevice
{
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:self.assetsFetchOptions];
    if (fetchResult.count <= 0) {
        [self showNoAssets];
        return;
    }

    NSMutableArray *fetchResults = [NSMutableArray new];

    for (NSNumber *subtypeNumber in self.assetCollectionSubtypes)
    {
        PHAssetCollectionSubtype subtype = subtypeNumber.integerValue;
        PHAssetCollectionType type = [self __assetCollectionTypeOfSubtype:subtype];

        PHFetchResult *fetchResult =
        [PHAssetCollection fetchAssetCollectionsWithType:type
                                                 subtype:subtype
                                                 options:self.assetCollectionFetchOptions];

        [fetchResults addObject:fetchResult];
    }

    self.fetchResults = fetchResults;

    if (fetchResults.count > 0) {
        [self getAlbumsCompletion:YES];
    }else{
        [self getAlbumsCompletion:NO];
    }
}

欢迎使用DLPhotoPicker开源库,封装了AssetsLibrary和Photos的API,支持iOS7和iOS8以上
源码下载:https://github.com/darling0825/DLPhotoPicker.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值