[英]How to save a video to the camera roll and get the asset URL?
我需要从iOS应用中导出视频并获取资产ID。 过去,您可以使用ALAssetsLibrary :
但是现在不推荐使用。 我想知道现在实现这一目标的新方法是什么? 谢谢!
已弃用ALAssetsLibrary。 您应该使用PhotosLibrary。
自iOS 9.0起不推荐使用Assets Library框架。 取而代之的是使用Photos框架,该框架在iOS 8.0和更高版本中为使用用户的照片库提供了更多功能和更好的性能。 有关更多信息,请参见照片。 在“照片”框架中,PHPhotoLibrary类管理对照片库的访问和更改,而PHAsset和PHCollection类以及相关类上的类方法提供查找照片和视频资产的功能。
看到这个https://developer.apple.com/documentation/photos/phphotolibrary
这是将视频保存到相机胶卷中的示例代码,并为您提供保存视频的网址。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.