OC获取并创建自定义相册

02-15 08:13 字数 829 阅读 460
- (PHAssetCollection *)createdCollection
{
    // 查询是否创建过相册
    PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

    // 获取APP名称
    NSString *appName = [NSBundle mainBundle].infoDictionary[@"CFBundleDisplayName"];

    // 遍历相册查看是否有和当前APP同名的相册
    for (PHAssetCollection *collection in result) {
        if ([collection.localizedTitle isEqualToString:appName]) {
            return collection;
        }
    }
    // 创建自定义相册
    __block NSString *identifier = nil;
    NSError *error = nil;

    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        identifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:appName].placeholderForCreatedAssetCollection.localIdentifier;
    } error:&error];

    if (error) return nil;

    return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[identifier] options:nil].firstObject;
}
0人点赞>
关注 收藏 改进 举报
0 条评论
排序方式 时间 投票
快来抢占一楼吧
请登录后发表评论