【问题标题】:Save video to a custom Album using Photos Framework in iOS使用 iOS 中的照片框架将视频保存到自定义相册
【发布时间】:2017-06-21 07:14:00
【问题描述】:

我想使用Photos Framework 将使用自定义相机拍摄的视频保存到特定相册中,例如Photos Library 中的NewAlbum。 我找到了一些答案,但他们提到了现在已弃用的ALAssetsLibrary 的使用。

如果您需要更多详细信息,请告诉我,如果我遗漏了什么,请纠正我。 谢谢

【问题讨论】:

    标签: ios swift ios9 photosframework


    【解决方案1】:

    我找到了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
    }
    

    您可以使用以下 sn-p 添加PHAsset

    代码块 2:

    // Consider we have a known localIdentifier for a specific PHAssetCollection defined as -> let collectionIdentifier: String
    guard let collection: PHAssetCollection = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [collectionIdentifier], options: nil).fistObject else {
       // handle Error
       return
    }
    PhotoLibrary.shared().performChanges({
    
        guard let addAssetRequest = PHAssetCollectionChangeRequest(for: collection) else { return }
    
        addAssetRequest.addAssets([newAsset] as NSArray)
    }, completionHandler: {
        success, error in
        //handle error or stuff you want to do after that here
    })
    

    编辑创建PHAssetCollection的示例

    在这种情况下,我在 performChanges(_:,completionHandler:) 闭包中创建创建 PHAssetCollection 后获取它,并将其放入该函数的 @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)
            }
        })
    

    【讨论】:

    • 谢谢。我会检查并通知您。
    • @Ankit Kumar Gupta 有效果吗?还是你还在苦苦挣扎?
    • 抱歉@Marcel 本周有其他优先事项。一定会测试它,并在下一个通知您。
    • @user25 当您看到第一个代码块时,您会看到 PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(/*Your url here*/) 就像在示例中一样,您需要获取占位符标识符。在 Completion Handler 中,使用 PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil).firstObject 获取创建的资产,其中 identifier 是占位符标识符,并像第二个代码块中提到的那样添加资产。我现在将用粗体文本标记块 :)
    • @user25 祝你好运,我会从你的搜索开始here,看起来很有希望,我以前没有做过这样的事情。
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多