【问题标题】:Image downloading with progress using AlamofireImage?使用 AlamofireImage 下载图像的进度?
【发布时间】:2016-02-02 07:17:19
【问题描述】:

有没有什么方法可以使用 AlamofireImage 下载图像并获得有关下载进度的某种反馈,同时利用它的 UIImage 扩展、图像过滤器和图像缓存的强大功能?

我知道我可以回退到普通的 Alamofire.request + responseImage,但我想保持简单并使用 UIImageView Extension

谢谢!

【问题讨论】:

  • @LokeshDudhat 问题与 AlamofireImage 有关。我已经在使用它,但我找不到跟踪图像下载进度的方法……

标签: ios swift alamofire alamofireimage


【解决方案1】:

目前没有任何方法可以在下载图像时使用 AlamofireImage 和 UIImageView 扩展来接收进度更新。最初没有添加它的原因是大多数用户似乎不需要这样的功能。我很想讨论更多,看看这是否是我们真正希望在未来版本中添加到 AlamofireImage 的功能。

您愿意提出一个贯穿您的用例的问题吗?我只是想确切地知道您希望它如何工作以及为什么您实际上需要进度报告。

【讨论】:

【解决方案2】:

用 Swift 3.0.2 试试这个:

let utilityQueue = DispatchQueue.global(qos: .utility)
let url = "http://kingofwallpapers.com/code/code-006.jpg"

    Alamofire.download(url)
        .downloadProgress(queue: utilityQueue) { progress in
            print("Download Progress: \(progress.fractionCompleted)")
        }
        .responseData { response in
            print("Response is \(response)")
            if let data = response.result.value {
                let image = UIImage(data: data)
            }
    }

【讨论】:

    【解决方案3】:

    在进程中使用 Alamofire 下载图片

    下载带有进度的文件

    Alamofire.download(.GET, "url...", destination: destination)
             .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                 print(totalBytesRead)
    
                 // This closure is NOT called on the main queue for performance
                 // reasons. To update your ui, dispatch to the main queue.
                 dispatch_async(dispatch_get_main_queue()) {
                     print("Total bytes read on main queue: \(totalBytesRead)")
                 }
             }
             .response { _, _, _, error in
                 if let error = error {
                     print("Failed with error: \(error)")
                 } else {
                     print("Downloaded file successfully")
                 }
             } 
    

    【讨论】:

    • 正如我在原帖中所说,«我知道我可以回退到一个简单的 Alamofire.request + responseImage 但我想保持简单并使用 UIImageView 扩展» 。这是为了利用缓存和其他有用的功能。
    猜你喜欢
    • 2017-01-19
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多