使用iOS中的照片框架将视频保存到自定义相册中
问题描述:
我想使用Photos Framework
将使用自定义相机拍摄的视频保存到Photos Library
中的某个相册中,例如NewAlbum
。 我找到了一些答案,但他们提到使用ALAssetsLibrary
现在已经弃用。使用iOS中的照片框架将视频保存到自定义相册中
请让我知道,如果您需要更多的细节,也纠正我,如果我错过了什么。 感谢
答
我不知道如何创建从视频对象PHAsset
但是当你可以做到这一点,
我找到了解决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
:
码块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
})
EDIT实施例用于创建PHAssetCollection
在这种情况下,我取出的创建PHAssetCollection
它在performChanges(_:,completionHandler:)
闭合创建之后,并把它在该功能的关闭@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库马尔古普塔是如此的工作?还是你还在与...打交道? –
对不起@Marcel本周还有其他一些优先事项。一定会测试它,并让你知道在下一个。 –