coding_cover676x469
Saving an image on a specific photo album seemed like a fairly easy task… IT WASN’T! After digging in Stackoverflow and Apple Documentation I finally was able to do it, so I thought I’d share with how it’s done! Or at least how I did it. ;)

For this Tutorial we’re going to use the Assets Library Framework. More specifically we will use an instance of the ALAssetsLibrary class.

So first thing to do is to import the AssetsLibrary framework and declare the instance of the ALAssetsLibrary in your view controller:

import AssetsLibrary
 
class YOURVIEWCONTROLLER: UIViewController{
    var library:ALAssetsLibrary =  ALAssetsLibrary()
}

No let’s go to the save function:

func saveImageToAlbum() {
  let image:UIImage = UIImage(named: "your_image_name")
  library.writeImageToSavedPhotosAlbum(image.CGImage, orientation: ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!, completionBlock:saveDone)
            }
}
 
func saveDone(assetURL:NSURL!, error:NSError!){
   println("saveDone")
  library.assetForURL(assetURL, resultBlock: self.saveToAlbum, failureBlock: nil)
 
}
 
func saveToAlbum(asset:ALAsset!){
 
   library.enumerateGroupsWithTypes(ALAssetsGroupAlbum, usingBlock: { group, stop in 
        stop.memory = false
        if(group != nil){
           let str = group.valueForProperty(ALAssetsGroupPropertyName) as! String
           if(str == "MY_ALBUM_NAME"){      
              group!.addAsset(asset!)
              let assetRep:ALAssetRepresentation = asset.defaultRepresentation()
              let iref = assetRep.fullResolutionImage().takeUnretainedValue()
              let image:UIImage =  UIImage(CGImage:iref)!
 
            }
 
        }
 
       },
       failureBlock: { error in
         println("NOOOOOOO")
   })
 
}

Pretty straight forward once you know how to do it.
Basically you save the image to your phone then loop through all the photo albums you have on the phone and if the name of the photo album is equal to the one you want your image to go then you just add the asset and BOOM, you win!