将视频录制到特定专辑

时间:2016-12-19 20:22:05

标签: swift3 xcode8 ios10 photolibrary photo-management

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

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

现在我想拍摄视频,并将其保存到相册中。我已经找到很多关于保存到相机胶卷,但没有保存到相册。可以这样做,如果是的话,怎么做?

1 个答案:

答案 0 :(得分:0)

假设您有一个PHAssetCollection指定相册,则可以使用此PHAssetCollection扩展名:

Counter({(8, 28): 1, (8, 33): 1, (8, 38): 1, (8, 43): 1, (13, 18): 1, (13, 23): 1, (13, 28): 1, (13, 33): 1, (13, 38): 1, (13, 43): 1, (18, 23): 1, (18, 28): 1, (18, 33): 1, (18, 38): 1, (18, 43): 1,(23, 28): 1, (23, 33): 1, (23, 38): 1, (23, 43): 1, (28, 33): 1, (28, 38): 1, (28, 43): 1, (33, 38): 1, (33, 43): 1, (38, 43): 1, (4, 9): 1, (4, 14):1, (4, 19): 1, (4, 24): 1, (4, 29): 1, (4, 34): 1, (4, 39): 1, (4, 44): 1, (9, 14): 1, (9, 19): 1, (9, 24): 1, (9, 29): 1, (25, 30): 1, (25, 35): 1, (25, 40): 1, (25, 45): 1, (30, 35): 1, (30, 40): 1, (30, 45): 1})

备注:

定义方法'isCameraRollAlbum'的原因是,发现整个相册都不能使用占位符,而只需要使用

PHAssetChangeRequest.creationRequestForAssetFromVideo

将视频保存到整个照片库。

不需要使用后台线程。

示例用法,假设名为“ Video.mov”的视频位于应用程序的“文档”目录中。这会将其保存到“相机胶卷”相册,但可以为任何相册指定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)
                }

            })
        }
    }
}

例如,您可以使用此扩展名来获取名称为'title'的相册的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))")
        }
    }

用法示例,将视频“ Video.mov”保存到名为“我的伞”的相册:

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)
        }

    }

}

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