Skip to content

Instantly share code, notes, and snippets.

@sazzadislam-dsi
Created January 27, 2017 08:33
Show Gist options
  • Save sazzadislam-dsi/d347909d284674c936e397ac423703cf to your computer and use it in GitHub Desktop.
Save sazzadislam-dsi/d347909d284674c936e397ac423703cf to your computer and use it in GitHub Desktop.
/**
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