Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
//declare closure outsize class definition (above class definition)
typealias CompletionHandler = (success:Bool!) -> Void
//declare library variable inside class
var library:ALAssetsLibrary?
//initialize variable in viewDidLoad
library = ALAssetsLibrary();
//call this method as :--
func savePhotoCustomAlbumName() {
let actuelPhoto = UIImage(named: "photo_full_01")
addImage(actuelPhoto, metadata: metaDataFromImage(actuelPhoto), toAlbum: "MyDreams") { (success) -> Void in
print("Image Added : \(success)");
// MARK: Meta Data From Image
func metaDataFromImage(image: UIImage) -> NSDictionary {
var jpeg: NSData = NSData(data: UIImagePNGRepresentation(image))
var source: CGImageSourceRef = CGImageSourceCreateWithData(jpeg, nil)
var imageMetaData: CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(source, 0, nil)
return imageMetaData
//method to add Image to Particular Album
func addImage(image:UIImage, metadata:NSDictionary, toAlbum albumName:String, handler:CompletionHandler){
library?.addAssetsGroupAlbumWithName(albumName, resultBlock: {(group:ALAssetsGroup!) -> Void in
print("\nAlbum Created:= \(albumName)");
/*-- Find Group --*/
var groupToAddTo:ALAssetsGroup?;
usingBlock: { (group:ALAssetsGroup?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
if(group != nil){
if group!.valueForProperty(ALAssetsGroupPropertyName) as! String == albumName{
groupToAddTo = group;
print("\nGroup Found \(group!.valueForProperty(ALAssetsGroupPropertyName))\n");
self.library?.writeImageToSavedPhotosAlbum(image.CGImage, metadata:metadata as [NSObject : AnyObject], completionBlock: {(assetURL:NSURL!,error:NSError!) -> Void in
if(error == nil){
resultBlock: { (asset:ALAsset!) -> Void in
var yes:Bool? = groupToAddTo?.addAsset(asset);
if (yes == true){
handler(success: true);
failureBlock: { (error2:NSError!) -> Void in
print("Failed to add asset");
handler(success: false);
} /*Group Is Not nil*/
failureBlock: { (error:NSError!) -> Void in
print("Failed to find group");
handler(success: false);
}, failureBlock: { (error:NSError!) -> Void in
print("Failed to create \(error)")
handler(success: false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.