I'm trying to save my images to a photo album specific to my app. I can create the album properly, but I can't save the images to it. I have the following code
-(void)save {
UIImage *imageToAddToAlbum = self.shapshot;
[self setupPhotoAlbumNamed:customPhotoAlbumName withCompletionHandler:
^(ALAssetsLibrary *assetsLibrary, ALAssetsGroup *group) {
if (group)
{
[self addImage:imageToAddToAlbum toAssetsLibrary:assetsLibrary withGroup:group];
}
}];
}
- (void) setupPhotoAlbumNamed: (NSString*) photoAlbumName withCompletionHandler:(void(^)(ALAssetsLibrary*, ALAssetsGroup*))completion
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
__weak ALAssetsLibrary *weakAssetsLibrary = assetsLibrary;
[assetsLibrary addAssetsGroupAlbumWithName:photoAlbumName resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"%@ Album result: %@", self, (group.editable ? @"success" : @"already existed"));
if (!group)
{
[weakAssetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *g, BOOL *stop) {
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customPhotoAlbumName])
{
completion(weakAssetsLibrary, g);
}
} failureBlock:^(NSError *error) {
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
completion(weakAssetsLibrary, nil);
}];
}
else
{
completion(weakAssetsLibrary, group);
}
} failureBlock:^(NSError *error)
{
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
completion(weakAssetsLibrary, nil);
}];
}
- (void) addImage: (UIImage*) image toAssetsLibrary: (ALAssetsLibrary*) assetsLibrary withGroup: (ALAssetsGroup*) group
{
[assetsLibrary writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:
^(NSURL *assetURL, NSError *error)
{
if (error)
{
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
}
else
{
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset)
{
[group addAsset:asset];
NSLog(@"%@ Image was succesfully added!", self);
} failureBlock:^(NSError *error) {
NSLog(@"%@ An error has occured with description: %@", self, error.localizedDescription);
}];
}
}];
}
EDIT!!
I found the answer on this other post. Sorry. Please close this.
It properly creates the Album but I it's not properly saving the images to the album I define.
Any help? Maybe a cleaner method of doing this?
EDIT:
The post that looks like a duplicate doesn't have an answer that satisfies iOS 8.0+ . I am now asking for a way to do this using the Photo library.