使用iOS中的照片框架将视频保存到自定义相册
我想使用使用iOS中的照片框架将视频保存到自定义相册,ios,swift,ios9,photosframework,Ios,Swift,Ios9,Photosframework,我想使用照片框架将使用自定义照相机拍摄的视频保存到照片库中的特定相册,例如新相册。 我已经找到了一些答案,但是他们提到了ALAssetsLibrary的使用,现在已经不推荐了 请让我知道,如果你需要任何更多的细节,也纠正我,如果我错过了什么。 谢谢我找到了一个解决方案: 代码块1: PhotoLibrary.shared().performChanges({ let createAssetRequest = PHAssetChangeRequest.creationRequestForA
照片框架将使用自定义照相机拍摄的视频保存到照片库
中的特定相册,例如新相册
。
我已经找到了一些答案,但是他们提到了ALAssetsLibrary
的使用,现在已经不推荐了
请让我知道,如果你需要任何更多的细节,也纠正我,如果我错过了什么。
谢谢我找到了一个解决方案:
代码块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
}
// 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
})
您可以使用以下代码段添加PHAsset
:
代码块2:
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
}
// 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
})
编辑创建阶段集合的示例
在本例中,我在performChanges(:,completionHandler:)
闭包中创建了createPHAssetCollection
,然后将其放入该函数的@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 Kumar Gupta那么它起作用了吗?还是你还在为某事苦苦挣扎?抱歉@Marcel本周还有其他一些优先事项。当然会测试它,并在下一个测试中让您知道。@user25当您看到第一个代码块时,您会看到PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(/*此处的url*/)
就像在示例中一样,您需要获取占位符标识符。在完成处理程序中,使用PHAsset.fetchAssets(withLocalIdentifier:[identifier],options:nil)获取创建的资产。firstObject
其中identifier
是占位符标识符,并添加资产,就像在第二个代码块中提到的那样。我现在将用粗体文字标记这些块:)@user25祝你好运,我将从你的搜索开始,它看起来很有希望,我以前从未做过类似的事情。