Add image to custom photo album

– (void) addAssetWithURL:(NSURL *) assetURL toPhotoAlbum:(NSString *) album
{
__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library assetForURL:assetURL
resultBlock:^(ALAsset *asset)
{
NSString *groupName = [[NSUserDefaults standardUserDefaults] objectForKey:@”groupURL”];
NSURL *groupURL = [[NSURL alloc] initWithString:groupName?groupName:@””];

[library groupForURL:groupURL
resultBlock:^(ALAssetsGroup *group)
{
NSString *groupName = [group valueForProperty:ALAssetsGroupPropertyName];

if ([album isEqualToString:groupName])
{
[group addAsset:asset];
}
else
{
__weak ALAssetsLibrary *lib = library;

[library addAssetsGroupAlbumWithName:album resultBlock:^(ALAssetsGroup *group)
{
NSString *groupName = [group valueForProperty:ALAssetsGroupPropertyName];
NSURL *groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];
[[NSUserDefaults standardUserDefaults] setObject:groupURL.absoluteString forKey:@”groupURL”];

[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if ([album isEqualToString:groupName])
{
[group addAsset:asset];
}

} failureBlock:^(NSError *error)
{
}];

} failureBlock:^(NSError *error)
{
}];
}

} failureBlock:^(NSError *error)
{
}];

} failureBlock:^(NSError *error)
{
}];
}

– (void) addToPhotoAlbum:(NSString *) album sourceType:(UIImagePickerControllerSourceType) sourceType assetURL:(NSURL *) assetURL
{
if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
{
[self addAssetWithURL:assetURL toPhotoAlbum:kAlbumName];
}
else
{
__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:self.CGImage
orientation:(ALAssetOrientation)self.imageOrientation
completionBlock:^(NSURL *assetURL, NSError *error)
{
[self addAssetWithURL:assetURL toPhotoAlbum:album];
}];
}
}

Advertisements