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

2 Responses to “Add image to custom photo album”

  1. Customized Photo Album Says:

    Congrats Kevin! Continue to inspire.

  2. Dinesh Says:

    Hi praveen,

    Thanks for your post . I need a help from you . I am trying an photo tag app where from the camera i captured the Image and created a custom album and stored the photo using AssetLibrary(already done) . Now when ever i store a photo inside custom album i want to store the photo with certain data/information .The need is later i want to get that only image using the data . Hope you got it.

    Thanks,
    Dinesh


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: