我正在从网络服务调用图像的 url.图像未加载到 uitableview [英] I am calling a url of the image from the webservice. The image is not loaded to uitableview

查看:74
本文介绍了我正在从网络服务调用图像的 url.图像未加载到 uitableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试代码的过程中,我测试了 URL,该 URL 在浏览器中有效,图像在浏览器中显示.但是下面的代码没有将图像加载到图像包装器中.

During the debugging of the code I tested the URL, that url works in the browser and the image is displayed in the browser. But the below code is not loading the image to the image wrapper.

let row = indexPath.row
cell.NewsHeading.font =
UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.NewsHeading.text = SLAFHeading[row]

if let url = NSURL(string: SLAFImages[indexPath.row]) {

     let task = NSURLSession.sharedSession().dataTaskWithURL(url) {  (data, response, error) -> Void in

     if error != nil {
         print("thers an error in the log")
     } else {

         dispatch_async(dispatch_get_main_queue()) {
             cell.NewsImage.image = UIImage(data: data!)

     }
    }

 }

 task.resume()    
}
 return cell

推荐答案

如评论中所述,您无法从异步任务返回 - 您无法知道何时该任务将完成以及数据何时可用.

As explained in the comments, you can't return from an asynchronous task - you can't know when the task will be complete and when the data will be available.

在 Swift 中处理这个问题的方法是使用回调,通常被称为完成处理程序".

The way to handle this in Swift is to use callbacks, often called by convention "completion handlers".

在这个例子中,我创建了一个函数来运行网络任务,这个函数在图像准备好时有一个回调.

In this example I create a function to run the network task, and this function has a callback for when the image is ready.

您使用名为尾随闭包"的语法调用此函数并在那里处理结果.

You call this function with a syntax named "trailing closure" and there you handle the result.

这里有一个例子.

新功能:

func getNewsImage(stringURL: String, completion: (image: UIImage)->()) {
    if let url = NSURL(string: stringURL) {
        let task = NSURLSession.sharedSession().dataTaskWithURL(url) {  (data, response, error) -> Void in
            if error != nil {
                print(error!.localizedDescription)
            } else {
                if let data = data {
                    if let image = UIImage(data: data) {
                        completion(image: image)
                    } else {
                        print("Error, data was not an image")
                    }
                } else {
                    print("Error, no data")
                }
            }
        }
        task.resume()
    }
}

示例中的元素:

let row = indexPath.row
cell.NewsHeading.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.NewsHeading.text = SLAFHeading[row]

以及如何调用新函数:

getNewsImage(SLAFImages[indexPath.row]) { (image) in
    dispatch_async(dispatch_get_main_queue()) {
        cell.NewsImage.image = image
        // here you update your UI or reload your tableView, etc
    }
}

这只是展示其工作原理的示例,因此您可能需要适应您的应用,但我相信它展示了您需要做的事情.

It's just an example to show how it works, so you might have to adapt to your app, but I believe it demonstrates what you need to do.

这篇关于我正在从网络服务调用图像的 url.图像未加载到 uitableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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