假设你有一个 PHAssetCollection 指定专辑,你可以使用这个 PHAssetCollection 扩展:
extension PHAssetCollection {
private func isCameraRollAlbum() -> Bool
{
let query = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: .smartAlbumUserLibrary,
options: nil)
let result: PHAssetCollection? = query.firstObject
return self == result
}
func save(videoURL: URL, completion: @escaping (URL?, String?) -> ()) {
let isCameraRoll = isCameraRollAlbum()
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now()) {
PHPhotoLibrary.shared().performChanges({
if let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL) {
if isCameraRoll == false, let placeholder = assetRequest.placeholderForCreatedAsset {
let albumChangeRequest = PHAssetCollectionChangeRequest(for: self)
albumChangeRequest?.addAssets([placeholder] as NSArray)
}
}
}, completionHandler: { (success, error) in
if success == false {
completion(nil, error?.localizedDescription)
}
else {
completion(videoURL, nil)
}
})
}
}
}
评论:
定义方法'isCameraRollAlbum'是因为发现对整个相册使用占位符不起作用,只需要使用
PHAssetChangeRequest.creationRequestForAssetFromVideo
将视频保存到整个照片库。
不需要使用后台线程。
示例用法,假设名为“Video.mov”的视频位于应用程序的 Documents 目录中。这会将其保存到“相机胶卷”相册,但可以指定任何相册的 PHAssetCollection:
let docsurl = try! FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let videoURL = docsurl.appendingPathComponent("Video.mov")
let fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumUserLibrary,options: nil)
if let allMediaAlbum = fetchResult.firstObject {
allMediaAlbum.save(videoURL: videoURL) { (url, message) in
print("message = \(String(describing: message))")
}
}
例如,您可以使用此扩展来获取具有给定名称“title”的专辑的 PHAssetCollection:
class func getAlbum(title: String, completionHandler: @escaping (PHAssetCollection?) -> ()) {
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now()) {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", title)
let collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let album = collections.firstObject {
completionHandler(album)
} else {
completionHandler(nil)
}
}
}
示例用法,将视频“Video.mov”保存到名为“My Umbrella”的专辑中:
let docsurl = try! FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let albumName = "My Umbrella"
let videoURL = docsurl.appendingPathComponent("Video.mov")
PHAssetCollection.getAlbum(title: albumName) { (album) in
if let album = album {
album.save(videoURL: videoURL, completion: { (url, error) in
if let url = url {
print("Video '\(url.lastPathComponent) saved to '\(albumName)'")
}
else {
print("Error: \(String(describing: error))")
}
})
}
}
(请记住,照片库可以有多个同名的相册。)