がここにあり、関数の手があなたが与えられた名前を使用してData
にDocumentDirectoryを保存することです。このメソッドには、保存アクションを実行した後に呼び出されるcall back
という1つがあり、ディレクトリに保存されたイメージのパスが返されます。この関数はSwift 3
で書かれています。
func saveImageInDocDirectory(data: Data?, fileName: String?, successblock: @escaping (_ path: String?) -> Void) { // To add the image to cache for given identifier.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = paths.appending("/\(fileName!)")
if !FileManager.default.fileExists(atPath: path) {
do {
try FileManager.default.createDirectory(at: URL(fileURLWithPath: path), withIntermediateDirectories: false, attributes: nil)
} catch {
print("Error creating images folder in Cache dir: \(error)")
}
}
if (filemgr.fileExists(atPath: path)) {
try! filemgr.removeItem(atPath: path)
} else {
do {
try data?.write(to: URL(fileURLWithPath: path, isDirectory: false))
successblock(path)
} catch {
successblock(nil)
print("Error while caching the data in cache folder.")
}
}
}
あなたは、のような
Alamofire.download(fileUrl, to: destination)
.downloadProgress { progress in
print("Download Progress:---------- \
(progress.fractionCompleted)")
}
.responseData { response in
print(response.request)
print(response.response)
print(response.temporaryURL)
print(response.destinationURL)
print(response.error)
if let data = response.result.value {
self.saveImageInDocDirectory(data: data, fileName: "YourFile", successblock: { (path) in
print(path)
}
}
}
おかげでこのメソッドを呼び出すことができます。
ダウンロードしたデータをカスタム名でアプリのドキュメントディレクトリに保存しますか? –
はい、ドキュメントディレクトリに異なる名前のファイルを保存します – mirza
私の答えを見てください。あなたがこの問題に直面した場合は私に教えてください。 –