gpt4 book ai didi

swift - Alamofire 下载问题

转载 作者:IT王子 更新时间:2023-10-29 05:13:14 26 4
gpt4 key购买 nike

我正在尝试下载 this picture在我的代码中使用 Alamofire 4.0.0 与 Xcode 8.0 和 Swift 3.0。

这是我的要求:

    func download(_ path: String, _ completionHandler: @escaping (Any?) -> ()) {
let stringURL = "https://slove.tulleb.com/uploads/6/6/0/2/66027561/2791411.jpg-1447979839.png"

print("Requesting \(stringURL)...")

_ = Alamofire.download(stringURL)
.responseData { response in
print(response)

if let data = response.result.value {
completionHandler(UIImage(data: data))
} else {
completionHandler(nil)
}
}
}

我从服务器得到以下答案:

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///private/var/mobile/Containers/Data/Application/50400F41-47FD-4276-8903-F48D942D064A/tmp/CFNetworkDownload_D1Aqkh.tmp))

我不知道如何解决这个问题...是 Alamofire 新版本有问题还是我忘记了什么地方?

谢谢!

最佳答案

Official answer from cnoon (Alamofire member):

Hi @Tulleb,

Apologies for not getting back to you sooner. The example @katopz is not the same type of request. That example demonstrates how to use a data task, not a download task. If you don't wish to download the file, you can instead do the following:

Alamofire.request(url).responseData { response in
guard let data = response.result.value else { return }
let image = UIImage(data: data)
print(image)
}

However, to answer you're original question, you're running into a sandbox permissions issue. We allow you to use the download APIs without specifying a destination closure for operating systems like macOS where you can access files outside of your own sandbox. However, on iOS, you cannot directly access the data of a file that is outside of your sandbox. That's why you are seeing the .inputFileReadFailed error.

There are a couple ways you can solve this issue.

Option 1

You can download the data using the request API as shown above which downloads the image data into memory, not to disk.

Option 2

You can move the file into your sandbox before accessing the data using a destination closure. Here's an example of how to do that:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsURL.appendingPathComponent("image.png")

return (fileURL, [.removePreviousFile, .createIntermediateDirectories]) }

Alamofire.download("https://httpbin.org/image/png", to:
destination).responseData { response in
debugPrint(response)

if let data = response.result.value {
let image = UIImage(data: data)
print(image)
} else {
print("Data was invalid")
}
}

// Outputs:

// [Request]: https://httpbin.org/image/png // [Response]: { URL: https://httpbin.org/image/png } { status code: 200, headers { // "Access-Control-Allow-Origin" = "*"; // "Content-Length" = 8090; // "Content-Type" = "image/png"; // Date = "Sat, 24 Sep 2016 21:34:25 GMT"; //
Server = nginx; // "access-control-allow-credentials" = true; // } } // [TemporaryURL]: /private/var/mobile/Containers/Data/Application/25612024-9A05-4ED5-AF3B-A98E22DEAD7A/tmp/CFNetworkDownload_fD9sXf.tmp // [DestinationURL]: /var/mobile/Containers/Data/Application/25612024-9A05-4ED5-AF3B-A98E22DEAD7A/Documents/image.png // [ResumeData]: 0 bytes // [Result]: SUCCESS: 8090 bytes // [Timeline]: Timeline: { "Request Start Time": 496445664.792, "Initial Response Time": 496445665.651, "Request Completed Time": 496445665.655, "Serialization Completed Time": 496445665.655, "Latency": 0.860 secs, "Request Duration": 0.863 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.864 secs } // Optional(, {100, 100}) You MUST use a destination closure if you need to download the file to disk. The temporary file can only be accessed inside the delegate callback which is handled internally in Alamofire. If you do not specify a destination closure on iOS, the temporaryURL will always point to where the temp file was previously stored, but has been cleaned up.

Summary

So in summary, if you don't need to download the data to disk, then you want Option 1. If you do want to save the file on disk, then you want Option 2.

Cheers. 🍻

关于swift - Alamofire 下载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com