【问题标题】:how to load images fast during scrolling in swift 3?如何在 swift 3 中滚动时快速加载图像?
【发布时间】:2018-01-18 05:47:57
【问题描述】:

我已经从 url 实现了一组图像并将其显示在集合视图和表格视图上,并在加载应用程序后部署在 iphone 5 设备中,图像页面没有快速滚动并且需要更多时间延迟如何避免这种情况任何人都可以帮忙我 ?

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return imageArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "productsCell", for: indexPath) as! productsCell
    let arr = imageArray[indexPath.row]
    let urls = NSURL(string: arr)
    let data = NSData (contentsOf: urls! as URL) //make sure your image in this url does exist, otherwise unwrap in a if let check
    cell.productImage.image = UIImage(data: data! as Data)
    cell.productName.lineBreakMode = NSLineBreakMode.byWordWrapping
    cell.productName.numberOfLines = 2
    cell.productName.text = self.productName[indexPath.row]
    cell.productPrice.text = self.productprice[indexPath.row]
    cell.buynowButton .addTarget(self, action: #selector(buyNowButton(_:)), for:UIControlEvents.touchUpInside)
    cell.cartButton .addTarget(self, action: #selector(cartButton(_:)), for:UIControlEvents.touchUpInside)
    return cell
}

【问题讨论】:

标签: ios uitableview swift3 uiimageview


【解决方案1】:

您可以使用 HanekeSwift。它会自动管理缓存,因此您的视图在加载大量图像时不会阻塞。 https://github.com/Haneke/HanekeSwift

希望这会有所帮助!

【讨论】:

  • 下载后如何实现呢? @Aditya Srivastava
  • 嗨,请阅读这个 github 链接,它在第一行给出了如何编码。
【解决方案2】:

我建议使用https://github.com/onevcat/Kingfisher Kingfisher 是一个轻量级的纯 Swift 库,用于从 Web 下载和缓存图像。它提供了一个内置的活动指示器,可以在图像视图中显示。 Kingfisher 确保来自完全相同 url 的图像只下载一次,它还提供了一种取消下载的方法,例如:当应该显示图像的单元格滚动到可见区域之外时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 2016-10-13
    • 2018-07-27
    • 1970-01-01
    • 2018-12-29
    • 2016-11-20
    相关资源
    最近更新 更多