Saving video to a custom album using the iOS framework

I want to save the video shot with a special camera to a specific album, say it NewAlbumin Photos Library, using Photos Framework. I found several answers, but they all referred to usage ALAssetsLibrary, which is deprecated now.

Please let me know if you need any details, also correct me if I missed something. Thanks

+4
source share
1 answer

I'm not sure how to create PHAssetfrom a Video object, but when you can do it,

I found a solution to create a PHAsset from the video :

1:

PhotoLibrary.shared().performChanges({
    let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(/*Your url here*/)
    placeholder = createAssetRequest.placeholderForCreatedAsset
    identifier = placeholder.localIdentifier
}, completionHandler: {
    success, error in 
    /* 
       Fetch Asset with the identifier here
       after that, add the PHAsset into an album
    */
    newAsset = PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil).firstObject
}

PHAsset :

2:

PhotoLibrary.shared().performChanges({

    guard let addAssetRequest = PHAssetCollectionChangeRequest(for: /*your specific album here as PHAssetCollection*/) else { return }

    addAssetRequest.addAssets([newAsset] as NSArray)
}, completionHandler: {
    success, error in
    //handle error or stuff you want to do after that here
})

EDIT PHAssetCollection

PHAssetCollection performChanges(_:,completionHandler:) @escaping .

PHPhotoLibrary.shared().performChanges({

        let createRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
        placeHolderIdentifier = createRequest.placeholderForCreatedAssetCollection.localIdentifier

    }, completionHandler: {
        success, error in
        if success {
            var createdCollection: PHAssetCollection? = nil
            if placeHolderIdentifier != nil {
                createdCollection = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeHolderIdentifier!], options: nil).firstObject
            }
            completion(success, createdCollection as? T)
        } else {
            LogError("\(error)")
            completion(success, nil)
        }
    })
+4

Source: https://habr.com/ru/post/1679774/


All Articles