AlamoFire下载问题 [英] Alamofire download issue

查看:42
本文介绍了AlamoFire下载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有Xcode 8.0和SWIFT 3.0的AlamoFire 4.0.0在我的代码中下载this picture

这是我的请求:

    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)
                }
        }
    }

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

故障: 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,

很抱歉没有早点给你回复。示例@katopz是 不是同一类型的请求。该示例演示如何使用 数据任务,而不是下载任务。如果您不想下载 文件,您可以改为执行以下操作:

Alamofire.request(url).responseData { response in
     guard let data = response.result.value else { return }
     let image = UIImage(data: data)
     print(image)
}
但是,为了回答您最初的问题,您遇到了沙箱权限问题。我们允许您使用 下载API,但不指定用于操作的目标闭包 像MacOS这样的系统,您可以在其中访问您自己的外部文件 沙盒。但是,在iOS上,您不能直接访问 您的沙箱之外的文件。这就是为什么你会看到 .inputFileReadFailed错误。

有几种方法可以解决此问题。

选项1

您可以使用上面所示的请求API下载数据, 将图像数据下载到内存中,而不是下载到磁盘中。

选项2

您可以在访问数据之前将文件移动到沙箱中 使用目的地闭包。以下是如何做到这一点的示例:

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")
    }
}

//输出:

//[请求]:https://httpbin.org/image/png//[响应]: {URL:https://httpbin.org/image/png }{状态码:200,标题{//"Access-Control-Allow-Origin"= "*";//"Content-Length"=8090;//"Content-Type"= "image/png";//date="星期六,2016年9月24日21:34:25 GMT";//
server=nginx;//"Access-Control-Allow-Credentials"=true;//} }//[临时URL]: /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字节//[结果]:成功:8090字节// [时间线]:时间线:{"请求开始时间":496445664.792,"初始 响应时间":496445665.651,"请求完成时间": 496445665.655,"序列化完成时间":496445665.655,"延迟":0.860秒,"请求时长":0.863秒,"序列化 持续时间":0.000秒,"总持续时间":0.864秒}// 可选(,{100,100})您必须使用 目标关闭(如果需要将文件下载到磁盘)。这个 临时文件只能在委托回调内访问,该委托回调 在阿拉莫火内部处理。如果您不指定 iOS上的目的地关闭,则临时URL将始终指向 临时文件以前存储但已清除的位置。

摘要

总而言之,如果您不需要将数据下载到磁盘,那么 您需要选项1。如果您确实要将文件保存在磁盘上,则您 需要选项%2。

干杯。🍻

这篇关于AlamoFire下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆