简体   繁体   中英

Photos and Videos are not saving in my Custom Photo Album

Photos and videos are not saving in my custom photo album (TestAlbum). Instead, it is saving in the Camera Roll. Can you kindly help me to identify the issue and help me to correct the code.

PHPhotoLibrary.shared().performChanges({
    let albumName = "TestAlbum"
    let assetCollection: PHAssetCollection!
    let fetchOptions = PHFetchOptions()
    PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)

    fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)

    let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

    if let _: AnyObject = collection.firstObject {
        collection.firstObject
    }

    let options = PHAssetResourceCreationOptions()
    options.shouldMoveFile = true

    let creationRequest = PHAssetCreationRequest.forAsset()

    creationRequest.addResource(with: .video, fileURL: outputFileURL, options: options)
}, completionHandler: { success, error in
    if !success {
        print("Could not save movie to photo library: \(String(describing: error))")
    }
    cleanUp()
})

As in the comments above I would separate the creation of the photo album and the saving of your photo/video into two performChanges blocks. But I don't see anyway in your code where you are actually adding the asset (photo/video) to your asset collection (photo album). To do that do as follows

let assetPlaceholder = creationRequest.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(for:collection)
if let changeRequest = albumChangeRequest, let placeholder = assetPlaceholder {
    changeRequest.addAssets(NSArray(array:[placeholder]))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM