如何将图像和pdf文件下载到Gallery和Files文件夹

时间:2018-07-15 07:28:01

标签: swift

这是我的代码,它下载文件但未将文件放置在图库或文件夹中:

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL:NSURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
            print("***documentURL: ",documentsURL)
            let fileURL = documentsURL.appendingPathComponent("\(ind).pdf")
            print("***fileURL: ",fileURL ?? "")
            return (fileURL!,[.removePreviousFile, .createIntermediateDirectories])
        }

    let parameters: Parameters = [:]
    Alamofire.download(urlString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)!,method:.get,parameters:parameters,encoding:JSONEncoding.default,headers:["Authorization": "\(ParentService.AUTHTOCK)"],to: destination).downloadProgress(closure: { (prog) in
            hud.progress = Float(prog.fractionCompleted)
        }).response { response in
            //print(response)
            hud.hide(animated: true)
            if response.error == nil, let filePath = response.destinationURL?.path {
                print("mmmm",filePath)

                self.fileLocalURLDict[ind] = filePath

                DispatchQueue.main.async {

                    let urlString:String! = self.fileLocalURLDict[ind]
                    let fileURL = URL(fileURLWithPath: urlString)
                    let request = URLRequest(url: fileURL)
                    self.webView.loadRequest(request)

                }


            }
            else
            {
                print(response.error)
            }
        }
        //--1.--//

   // }
}

0 个答案:

没有答案