ios创建相册

- (IBAction)tt:(id)sender {
NSLog(@"%@",@"FF");


[self insertImage: NULL intoAlbumNamed:@"bbbbsssss"];
}


- (void)insertImage:(UIImage *)image intoAlbumNamed:(NSString *)albumName {
//Fetch a collection in the photos library that has the title "albumNmame"
PHAssetCollection *collection = [self fetchAssetCollectionWithAlbumName: albumName];

if (collection == nil) {
    //If we were unable to find a collection named "albumName" we'll create it before inserting the image
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: albumName];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Error inserting image into album: %@", error.localizedDescription);
        }
        
        if (success) {
            //Fetch the newly created collection (which we *assume* exists here)
            PHAssetCollection *newCollection = [self fetchAssetCollectionWithAlbumName:albumName];
    //                [self insertImage:image intoAssetCollection: newCollection];
        }
    }];
} else {
    //If we found the existing AssetCollection with the title "albumName", insert into it
//        [self insertImage:image intoAssetCollection: collection];
    }
}

- (PHAssetCollection *)fetchAssetCollectionWithAlbumName:(NSString *)albumName {
PHFetchOptions *fetchOptions = [PHFetchOptions new];
//Provide the predicate to match the title of the album.
fetchOptions.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"title == '%@'", albumName]];

//Fetch the album using the fetch option
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];

//Assuming the album exists and no album shares it's name, it should be the only result fetched
return fetchResult.firstObject;
}

你可能感兴趣的:(ios创建相册)