如何将视频文件移动到照片中的特定相册?

时间:2018-02-20 11:59:28

标签: ios swift

我可以将视频文件移动到照片中的根目录,但我想将其移动到特定的相册(localIdentifier

将视频文件移动到照片根目录的功能(需要编辑它才能移动到照片的特定相册 - localIdentifier)

PHPhotoLibrary.shared().performChanges({
                print("performChanges")
                let options = PHAssetResourceCreationOptions()
                options.shouldMoveFile = true

                let creationRequest = PHAssetCreationRequest.forAsset()

                creationRequest.addResource(with: .video, fileURL: self._VideoFileURL, options: options)
            }, completionHandler: { success, error in
                if !success {
                    print("Could not save movie to photo library: \(String(describing: error))")
                } else {
                    print("Saved movie to photo library")
                }
            }

1 个答案:

答案 0 :(得分:0)

以下是我用来检查文件是否已存在的方法。

 static func isAlreadyExists(fileName:String) -> Bool {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let filePath = documentsDirectory.appendingPathComponent(Constants.AppName).appendingPathComponent(fileName)

    if (FileManager.default.fileExists(atPath: filePath.path)){
        return true
    }else{
        return false
    }

}

以下是将文件保存到特定文件夹

的代码
  static func createDirectory() -> URL {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let DocumentsDirectory = documentsDirectory.appendingPathComponent(Constants.AppName)

    if !(FileManager.default.fileExists(atPath: DocumentsDirectory.path)){
        do {
            try FileManager.default.createDirectory(atPath: DocumentsDirectory.path, withIntermediateDirectories: true, attributes: nil)
        } catch let error as NSError {
            print(" Error creating directory: \(error.localizedDescription)")
        }
    }
    return DocumentsDirectory

}

希望这对你有用。