加载图像异步并返回UIImage的公共函数

问题描述 投票:1回答:1

我正在尝试编写加载图像异步并返回UIImage的公共函数。我想用它来填充UITableView图像。

class CommonFunctions {

func loadImageAsync(StringURL: NSString) -> UIImage{

    var returningImage = UIImage()

    let url = NSURL(string: StringURL)

    let requestedURL = NSURLRequest(URL: url!)

    NSURLConnection.sendAsynchronousRequest(requestedURL, queue: NSOperationQueue.mainQueue(), completionHandler: {
        response, data, error in

        if error != nil {
            println("there is some error loading image")
        }
        else {

            if let image = UIImage(data: data){
                returningImage = image
            }
        }
    })
    return returningImage
    }

}

问题是,当我想使用这个功能时:

cell.imageModelImage.image = CommonFunctions.loadImageAsync(CommonFunctions)

而不是String参数我得到了这个类?为什么会这样?

swift asynchronous uiimage load
1个回答
2
投票

你问:

而不是String参数我得到了这个类?为什么会这样?

这是因为你正在调用它,好像它是一个类函数,但没有这样定义它。如果在函数声明中添加class关键字,则不会看到该行为。

但是这里存在更深层次的问题:您无法从异步函数返回值(因为该函数将立即返回,而异步请求将在稍后完成)。

一种解决方案是提供一个完成处理程序,如果成功检索到图像,将会调用该处理程序:

class func loadImageAsync(stringURL: String, completion: @escaping (UIImage?, Error?) -> Void) {
    let url = URL(string: stringURL)!

    URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data, error == nil else {
            DispatchQueue.main.async { completion(nil, error) }
            return
        }
        let image = UIImage(data: data)
        DispatchQueue.main.async { completion(image, nil) }
    }.resume()
}

注意,我正在将完成处理程序分派回主队列,在那里应该进行所有UI更新。

然后,在tableView(_:cellForRowAt:)中,您可以提供在异步请求完成时将调用的块:

cell.imageModelImage.image = ... // remember to initialize image view before starting async method
CommonFunctions.loadImageAsync(with: "http://example.com/test.jpg") { image, error in
    if let cell = tableView.cellForRow(at: indexPath) as? CustomCell {  // make sure cell hasn't scrolled out of view
        cell.imageModelImage.image = image
    }
}

注意,上面假设在中间时间段内不可能在表格中插入一行。如果该假设无效,而不是使用旧的indexPath,则可能需要重新查询模型以确定在调用异步完成处理程序时IndexPath对此单元格有效。

对于Swift 2的演绎,请参阅此答案的previous revision

© www.soinside.com 2019 - 2024. All rights reserved.