# OC获取并创建自定义相册

**URL:** <https://www.shiqidu.com/t/topic/548>\
**Category:** 开发调优\
**Tags:** C-C-Obj-C\
**Created:** [2019 年2 月 15 日 00:13 UTC](https://www.shiqidu.com/t/topic/548 "2019-02-15T00:13:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2019 年2 月 15 日 00:13 UTC](https://www.shiqidu.com/t/topic/548/1 "2019-02-15T00:13:19Z")

</div>

```oc
- (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;
}

```
