【问题标题】:How to save a video to the camera roll and get the asset URL?如何将视频保存到相机胶卷并获取资产 URL?
【发布时间】:2017-06-03 12:42:10
【问题描述】:

我需要从我的 iOS 应用中导出视频并获取资产 ID。过去,您可以使用 ALAssetsLibrary

save image in camera roll and get asset url

但它现在已被弃用。我想知道现在实现这一目标的新方法是什么?谢谢!

【问题讨论】:

    标签: ios phasset alasset phfetchoptions phassetslibrary


    【解决方案1】:

    ALAssetsLibrary 已弃用。你应该使用 PhotosLibrary。

    自 iOS 9.0 起,Assets Library 框架已被弃用。相反,请改用 Photos 框架,它在 iOS 8.0 及更高版本中为使用用户的照片库提供了更多功能和更好的性能。有关详细信息,请参阅照片。 在 Photos 框架中,PHPhotoLibrary 类管理对照片库的访问和更改,PHAsset 和 PHCollection 类和相关类上的类方法提供查找照片和视频资产的功能。

    看到这个https://developer.apple.com/documentation/photos/phphotolibrary

    这是一个将视频保存到相机胶卷的示例代码,并为您提供保存视频的 url。

    var placeHolder : PHObjectPlaceholder?
    PHPhotoLibrary.shared().performChanges({
    
        let creationRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(string: videoPath)! )
        placeHolder = creationRequest?.placeholderForCreatedAsset
    
        }, completionHandler: { (success, error) in
            if success {
    
                let result = PHAsset.fetchAssets(withLocalIdentifiers: [placeHolder!.localIdentifier], options: nil)
                result.firstObject?.getURL(completionHandler: { url in
    
                    // this is the url to the saved asset 
    
                })
            }
    })
    

    别忘了

    import Photos
    

    并将此代码添加为 PHAsset 的扩展:

    https://gist.github.com/jVirus/2f041bb8b1936093773f0bde42af3a49

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 2015-05-12
    • 2016-05-01
    • 2015-01-06
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多