How to create photo album in iOS swift?

Try the following code-

 

func createPhotoLibraryAlbum(name: String) {

        var albumPlaceholder: PHObjectPlaceholder?

        PHPhotoLibrary.shared().performChanges({

            // Request creating an album with parameter name

            let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)

            // Get a placeholder for the new album

            albumPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection

        }, completionHandler: { success, error in

            if success {

                guard let placeholder = albumPlaceholder else {

                    fatalError("Album placeholder is nil")

                }

                

                let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)

                guard let album: PHAssetCollection = fetchResult.firstObject else {

                    // FetchResult has no PHAssetCollection

                    return

                }

                print("album created sucessfully")

                // Saved successfully!

                print(album.assetCollectionType)

            }

            else if let e = error {

                print(e.localizedDescription)

                // Save album failed with error

            }

            else {

                 print("cannot create photo album")

                // Save album failed with no error

            }

        })

    }
Back to Top