Skip to content

Instantly share code, notes, and snippets.

@acmacalister
Created December 9, 2013 21:41
Show Gist options
  • Save acmacalister/7881496 to your computer and use it in GitHub Desktop.
Save acmacalister/7881496 to your computer and use it in GitHub Desktop.
//
// ALAssetsLibraryUtil
//
// Created by Marin Todorov on 10/26/11.
// Modified by sapzildj@gmail.com
//
// Copyright (c) 2011 Marin Todorov. All rights reserved.
// original source from http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
//
#import "ALAssetsLibraryUtil.h"
@implementation ALAssetsLibraryUtil
// this method for ios5 asset library has lifetime
+ (ALAssetsLibrary *)defaultAssetsLibrary
{
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
// ios5.0 has bug when access to asset or UIImagePicker(ios 5.1 fixed)
// wrap with try/catch and library is nil then pred = 0 to rerun dispatch_once
// reference : https://devforums.apple.com/message/568023#568023
// reference : http://stackoverflow.com/questions/7701578/app-crashes-when-trying-to-obtain-photos-from-photo-library-via-image-picker
@try
{
library = [[ALAssetsLibrary alloc] init];
}
@catch (NSException *e)
{
LOG_OBJECT(e);
}
});
if (!library)
{
pred = 0;
}
return library;
}
// UIImageWriteToSavedPhotosAlbum callback
+ (void)didImageSavedPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
LOG_METHOD();
SaveImageCompletion completionBlock = (SaveImageCompletion)contextInfo;
completionBlock(error);
[completionBlock release];
}
+ (void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
// iOS 5 or Later
BOOL bIOS5 = ([[[UIDevice currentDevice] systemVersion] compare:@"5.0"] != NSOrderedAscending) ? YES : NO;
if(bIOS5)
{
ALAssetsLibraryWriteImageCompletionBlock writeImageBlock = ^(NSURL* assetURL, NSError* error)
{
LOG_METHOD();
if (error)
{
completionBlock(error);
}
else
{
//add the asset to the custom photo album
[self addAssetURL:assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}
};
//write the image data to the assets library (camera roll)
[[self defaultAssetsLibrary] writeImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:writeImageBlock];
}
else
{
UIImageWriteToSavedPhotosAlbum(image, self, @selector(didImageSavedPhotosAlbum:didFinishSavingWithError:contextInfo:), [completionBlock copy]);
}
}
+ (void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
LOG_METHOD();
__block BOOL albumWasFound = NO;
[[self defaultAssetsLibrary] assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
LPLog(@"###### asset:%@", asset);
//search all photo albums in the library
[[self defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare:[group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame)
{
if ([group isEditable] == NO || [group addAsset:asset] == NO)
{
LPLog(@"###### addAsset fail %d", [group isEditable]);
}
albumWasFound = YES;
completionBlock(nil);
//album was found, bail out of the method
*stop = YES;
}
else if (!group && albumWasFound == NO)
{
ALAssetsLibraryGroupResultBlock groupAddBlock = ^(ALAssetsGroup *addedGroup)
{
LPLog(@"###### groupAddBlock");
if ([addedGroup isEditable] == NO || [addedGroup addAsset:asset] == NO)
{
LPLog(@"###### addAsset fail %d", [addedGroup isEditable]);
}
completionBlock(nil);
};
//create new assets album
[[self defaultAssetsLibrary] addAssetsGroupAlbumWithName:albumName
resultBlock:groupAddBlock
failureBlock:completionBlock];
*stop = YES;
}
} failureBlock:completionBlock];
} failureBlock:completionBlock];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment