Swift3 将视频录制到特定相册

Swift3 将视频录制到特定相册,swift3,xcode8,ios10,photolibrary,photo-management,Swift3,Xcode8,Ios10,Photolibrary,Photo Management,我正在用户照片库中创建相册,现在我想在那里保存一个视频。我正在使用以下命令将视频保存到文件: let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] let filePath = documentsURL.URLByAppendingPathComponent("video") 现在我想拍摄视频,并将其保存到相册中。

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

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

现在我想拍摄视频,并将其保存到相册中。我发现了很多关于保存到相机卷的方法,但是没有发现任何关于保存到相册的方法。可以这样做吗?如果可以,怎么做?

假设您有一个指定相册的PhaseSetCollection,您可以使用此PhaseSetCollection扩展:

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”是因为发现对整个相册使用占位符无效,您只需要使用

PhaseSetChangeRequest.creationRequestForAssetFromVideo

将视频保存到整个照片库

不需要使用背景线程

例如,假设应用程序的文档目录中有一个名为“video.mov”的视频。这会将其保存到“摄影机滚动”相册,但可以指定任何相册的PhaseSetCollection:

    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”的相册的PhaseSetCollection:

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”保存到名为“我的雨伞”的相册中:

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

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

你忘了附加mime类型(文件扩展名)@LeoDabus-虽然我确实忘了,但你的评论根本无法回答我的问题。最后,一个我在过去3天里一直在寻找的答案!!!谢谢