【发布时间】:2017-06-21 07:14:00
【问题描述】:
我想使用Photos Framework 将使用自定义相机拍摄的视频保存到特定相册中,例如Photos Library 中的NewAlbum。
我找到了一些答案,但他们提到了现在已弃用的ALAssetsLibrary 的使用。
如果您需要更多详细信息,请告诉我,如果我遗漏了什么,请纠正我。 谢谢
【问题讨论】:
标签: ios swift ios9 photosframework
我想使用Photos Framework 将使用自定义相机拍摄的视频保存到特定相册中,例如Photos Library 中的NewAlbum。
我找到了一些答案,但他们提到了现在已弃用的ALAssetsLibrary 的使用。
如果您需要更多详细信息,请告诉我,如果我遗漏了什么,请纠正我。 谢谢
【问题讨论】:
标签: ios swift ios9 photosframework
我找到了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)
}
})
【讨论】:
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(/*Your url here*/) 就像在示例中一样,您需要获取占位符标识符。在 Completion Handler 中,使用 PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil).firstObject 获取创建的资产,其中 identifier 是占位符标识符,并像第二个代码块中提到的那样添加资产。我现在将用粗体文本标记块 :)