swift3 - 将视频录制到特定相册

标签 swift3 xcode8 ios10 photolibrary photo-management

我正在用户照片库中创建一个相册,现在我想在那里保存一个视频。我正在使用以下方法将视频保存到文件中:

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let filePath = documentsURL.URLByAppendingPathComponent("video")

现在我想拍摄视频,并将其保存到相册中。我发现很多关于保存到相机胶卷的内容,但没有找到关于保存到相册的内容。可以做到吗?如果可以,怎么做?

最佳答案

假设您有一个指定相册的 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”的视频位于应用程序的文档目录中。这会将其保存到“相机胶卷”相册,但可以指定任何相册的 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))")
                }
            })
        }
    }

(请记住,照片库可以有多个同名相册。)

关于swift3 - 将视频录制到特定相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230407/

相关文章:

ios - 调用放置在数组中的函数

ios - 使用 Alamofire 解析 json 时出现 AppDelegate 错误

ios - swift : Value of type has no member 'peformSelector'

ios - 不同 iOS iPhone 尺寸的全屏图像

swift - 我如何在 XCODE 8 和 Swift 3 中使用 UIAutomation

ios - 内容安全策略框架祖先。 Iframe 不会在 iOS10 中加载内容

iOS 10 本地通知 : show notification content on lock screen

ios - 在 Swift 中为完整的 UITableView 创建模糊效果

ios - Xcode 8 自动 NSManagedObject 子类代码生成与 transient 属性

ios - Decifrate 用户崩溃日志文件 iOS