You were on the right track to create a new group in AssetsLibrary. Here is how you can add the photo there :
First you need to write the photo to Standard Assets Library (Camera Roll)
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL* assetURL, NSError* error)
{
if (!error) {
__block BOOL albumAlredyCreated = NO;
// Check if Album already exists
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([customAlbum compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
{
albumAlredyCreated = YES;
//Get the ALAsset instance for the saved image
[library assetForURL: assetURL
resultBlock:^(ALAsset *asset)
{
//Save photo to customAlbum
[group addAsset: asset];
} failureBlock:^(NSError *error) {
// If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
// If the data is currently unavailable, the failure block will be called.
}];
}
// Album does not exist. Create it
if (group == nil && albumAlredyCreated == NO)
{
__weak ALAssetsLibrary* weakLibrary = library;
[library addAssetsGroupAlbumWithName:customAlbum
resultBlock:^(ALAssetsGroup *group)
{
//Get the ALAsset instance for the saved image
[weakLibrary assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//Save photo to customAlbum
[group addAsset: asset];
} failureBlock:^(NSError *error) {
// If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
// If the data is currently unavailable, the failure block will be called.
}];
} failureBlock:^(NSError *error) {
// If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
}];
}
} failureBlock:^(NSError *error) {
// If the user denies access to the application, or if no application is allowed to access the data, the failureBlock is called.
}];
}
}];
Although this would work but ALAssetsLibrary
is depricated in iOS 9, so you may want to consider moving to PHPhotoLibrary
.
NS_CLASS_DEPRECATED_IOS(4_0, 9_0, "Use PHPhotoLibrary from the Photos
framework instead") @interface ALAssetsLibrary : NSObject