Created
May 30, 2015 22:04
-
-
Save burhanaksendir/498079a9a4c60c66b52c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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?; | |
self.library?.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum), | |
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){ | |
self.library?.assetForURL(assetURL, | |
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