【问题标题】:Downloading to camera roll using alamofire使用 alamofire 下载到相机胶卷
【发布时间】:2017-11-28 21:28:39
【问题描述】:

我正在使用此功能使用 alamofire 将视频下载到名为 downloads 的文件中。 我将如何编辑它以便将视频保存到相机胶卷

    func downloadVideoToCameraRoll() {


    let destination: DownloadRequest.DownloadFileDestination = { _, response in
        let pathComponent = response.suggestedFilename!
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
        let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
        return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    Alamofire.download(firstId, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination)
        .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
            self.progresss.setProgress(Float(progress.fractionCompleted), animated: true)
            //print("Progress: \(progress.fractionCompleted)")

        }
        .validate { request, response, temporaryURL, destinationURL in
            // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
            return .success
        }
        .responseJSON { response in
            debugPrint(response)
            print(response.temporaryURL!)
            print(response.destinationURL!)
    }

【问题讨论】:

标签: ios swift alamofire


【解决方案1】:

试试下面的代码

func downloadVideoToCameraRoll() {


    let destination: DownloadRequest.DownloadFileDestination = { _, response in
        let pathComponent = response.suggestedFilename!
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

        return (directoryURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    Alamofire.download(firstId, method: .get, parameters: nil, encoding: JSONEncoding.default, to: destination)
        .downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
            self.progresss.setProgress(Float(progress.fractionCompleted), animated: true)
            //print("Progress: \(progress.fractionCompleted)")

        }
        .validate { request, response, temporaryURL, destinationURL in
            // Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
            return .success
        }
        .responseJSON { response in
            debugPrint(response)
            print(response.temporaryURL!)
            print(response.destinationURL!)
            saveVideoTo(destinationURL)
    }


    func saveVideoTo(_ videoUrl:Url?){

    if videoUrl != nil {
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

            let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: videoUrl)!)!
            createAssetRequest.placeholderForCreatedAsset

            }) { (success, error) -> Void in
                if success {
                //saved successfully 

                }
                else {
                 //error occured
                }
        }

    }

    }

【讨论】:

  • 嗨。我在您的代码中收到 2 个红色警告:“使用未声明的类型 'Url'”和“使用未声明的类型 'destinationURL'”。你能帮我吗? Tks
【解决方案2】:

你可以在 swift 3 中使用它:

PHPhotoLibrary.shared().performChanges({
   PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: urlToYourVideo)
}) { saved, error in
if saved {
    print("Saved")
}
}

注意:需要导入 照片

【讨论】:

  • 以及如何检索已保存的资产?
猜你喜欢
  • 2013-07-23
  • 1970-01-01
  • 2015-09-25
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多