本文介绍了使用iOS中的Photos Framework将视频保存到自定义相册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将使用自定义相机拍摄的视频保存到照片库中的 NewAlbum 使用照片框架
我找到了一些答案,但他们提到使用现在已弃用的 ALAssetsLibrary

I want to save a video that is taken using a custom camera to a particular album say NewAlbum in the Photos Library using the Photos Framework.I have found some answers but they referred to use of ALAssetsLibrary which is deprecated now.

如果您需要更多细节,请告诉我,如果我错过了某些内容,请纠正我。
谢谢

Kindly let me know if you need any more details, also rectify me if I missed something.Thanks

推荐答案

我找到了一个解决方案To :

I found a solution To create a PHAsset from an 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 ,包含以下代码段:

you can add the PHAsset with the following snippet:

代码区块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
})

编辑创建示例PHAssetCollection

EDIT Example for create a PHAssetCollection

在这种情况下,我获取了创建 PHAssetCollection performChanges(_:,completionHandler:)闭包中创建后,将其放入 @escaping 关闭该函数。

In this case, I fetched the create PHAssetCollection after it was created in the performChanges(_:,completionHandler:) closure and put it in the @escaping closure of that function.

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)
        }
    })

这篇关于使用iOS中的Photos Framework将视频保存到自定义相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:15