如何更新图像视图?

时间:2016-02-07 01:58:27

标签: ios swift

我从我的服务器抓取我的图像,从我的数据库中抓取URL,并在我的自定义tableview单元格中显示它。

我正在使用下面的扩展程序将网址转换为图片。正如您所看到的那样,它是异步获取图像,但它不显示图像。我检查了URL是否有效,但它没有显示

在我使用NSJSONSerialization获取数据后,我做了一个

dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData()
                    })

这就是我在cellForRowAtIndexPath

中调用它的方法
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:LatestPostCustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("LatestPostCustomTableViewCell", forIndexPath: indexPath) as! LatestPostCustomTableViewCell

        cell.imagePost.downloadedFrom(link: latestPost[indexPath.row].imageURL, contentMode: .ScaleAspectFit)

        return cell
    }
}

我正在使用的Loading/Downloading image from URL on Swift

检索到的扩展程序
extension UIImageView {
    func downloadedFrom(link link:String, contentMode mode: UIViewContentMode) {
        guard
            let url = NSURL(string: link)
            else {return}
        contentMode = mode
        NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
            guard
                let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
                let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
                let data = data where error == nil,
                let image = UIImage(data: data)
                else { return }
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                self.image = image
            }
        }).resume()
    }
}

0 个答案:

没有答案