如何使用swift在表视图中实现延迟加载图像 [英] how to implement lazy loading of images in table view using swift

查看:93
本文介绍了如何使用swift在表视图中实现延迟加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用swift在我的表视图中使用延迟加载概念。在我的表视图中,我显示了多个包含产品图像和产品名称的单元格。
请帮我解决问题。

I want to use lazy loading concept for my table view using swift. In my table view i am showing multiple cells which contain product images and product name . Please help me to get the solution.

推荐答案

旧解决方案:

由于您没有显示任何代码。

Since you doesn't show any code.

以下是您的示例。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // try to reuse cell
    let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("DealCell") as CustomCell

    // get the deal image
    let currentImage = deals[indexPath.row].imageID
    let unwrappedImage = currentImage
    var image = self.imageCache[unwrappedImage]
    let imageUrl = NSURL(string: "http://staging.api.cheapeat.com.au/deals/\(unwrappedImage)/photo")

    // reset reused cell image to placeholder
    cell.dealImage.image = UIImage(named: "placeholder")

    // async image
    if image == nil {

    let request: NSURLRequest = NSURLRequest(URL: imageUrl!)

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
        if error == nil {

            image = UIImage(data: data)

            self.imageCache[unwrappedImage] = image
            dispatch_async(dispatch_get_main_queue(), {
                cell.dealImage.image = image

            })
        }
        else {

        }
    })
    }

    else{
        cell.dealImage.image = image
    }

  return cell

}

关注这个教程更多信息。希望这对你有所帮助。

Follow THIS tutorial for more Info. Hope this will help you.

新解决方案:

这是一个扩展名它是由我的朋友 Leo Dabus 创建的,它非常简单易用:

Here is extension for it which is created by my friend Leo Dabus which is really simple to use:

extension UIImageView {
    func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
        NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
            (data, response, error) -> Void in
            dispatch_async(dispatch_get_main_queue()) {
                self.contentMode =  contentMode
                if let data = data { self.image = UIImage(data: data) }
            }
        }).resume()
    }
}

现在在 cellForRowAtIndexPath 方法中将图像分配给单元格:

Now in your cellForRowAtIndexPath method assign image to cell this way:

cell.cellImageView.image = UIImage(named: "placeholder")  //set placeholder image first.
cell.cellImageView.downloadImageFrom(link: imageLinkArray[indexPath.row], contentMode: UIViewContentMode.ScaleAspectFit)  //set your image from link array.

并且 Rob 建议在这里评论一些有用的库你可以使用:

And as Rob suggested into comment here is some useful libraries which you can use:


  1. https://github.com/Alamofire/AlamofireImage

  2. https://github.com/onevcat/Kingfisher

  3. https://github.com/rs/SDWebImage

  4. https://github.com/kean/DFImageManager

  1. https://github.com/Alamofire/AlamofireImage
  2. https://github.com/onevcat/Kingfisher
  3. https://github.com/rs/SDWebImage
  4. https://github.com/kean/DFImageManager

这篇关于如何使用swift在表视图中实现延迟加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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