Created
January 27, 2017 08:33
-
-
Save sazzadislam-dsi/d347909d284674c936e397ac423703cf 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
/** | |
Create and return an album in the Photos app with a specified name. Won't overwrite if such an album already exist. | |
- parameter named: Name of the album. | |
- parameter completion: Called in the background when an album was created. | |
*/ | |
public static func createAlbum(named: String, completion: (album: PHAssetCollection?) -> ()) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { | |
var placeholder: PHObjectPlaceholder? | |
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ | |
let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(named) | |
placeholder = createAlbumRequest.placeholderForCreatedAssetCollection | |
}) { success, error in | |
var album: PHAssetCollection? | |
if success { | |
let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([placeholder?.localIdentifier ?? ""], options: nil) | |
album = collectionFetchResult.firstObject as? PHAssetCollection | |
} | |
completion(album: album) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment