【发布时间】:2017-06-03 12:42:10
【问题描述】:
我需要从我的 iOS 应用中导出视频并获取资产 ID。过去,您可以使用 ALAssetsLibrary:
save image in camera roll and get asset url
但它现在已被弃用。我想知道现在实现这一目标的新方法是什么?谢谢!
【问题讨论】:
标签: ios phasset alasset phfetchoptions phassetslibrary
我需要从我的 iOS 应用中导出视频并获取资产 ID。过去,您可以使用 ALAssetsLibrary:
save image in camera roll and get asset url
但它现在已被弃用。我想知道现在实现这一目标的新方法是什么?谢谢!
【问题讨论】:
标签: ios phasset alasset phfetchoptions phassetslibrary
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
【讨论】: