0

I had implemented a set of images from url and displaying it on collection view and table view and deployed in the iphone 5 device after loading app the images page was not scrolling fast and taking more time delay how to avoid this can anyone help me ?

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
}
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
  • First you have to download all images in cache and when scrolling at that time get images from cache. It can be memory issue in future. – Shah Nilay Aug 10 '17 at 06:22
  • how to download images in cache i don't know i am new to swift 3 ?@NilayShah –  Aug 10 '17 at 06:23
  • Possible duplicate of [how to implement lazy loading of images in table view using swift](https://stackoverflow.com/questions/28694645/how-to-implement-lazy-loading-of-images-in-table-view-using-swift) – Devang Tandel Aug 10 '17 at 06:25
  • pls refer link. It is in Objective C. https://stackoverflow.com/a/14961854/1746086 – Shah Nilay Aug 10 '17 at 06:25
  • if possible please post the code for swift 3 @NilayShah –  Aug 10 '17 at 06:27

2 Answers2

0

You can use HanekeSwift. It automatically manages caching so your view won't block while loading heavy Images. https://github.com/Haneke/HanekeSwift

Hope this helps!

Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
0

I suggest to use https://github.com/onevcat/Kingfisher Kingfisher is a lightweight, pure-Swift library for downloading and caching images from the web. It provides a build in activity indicator which can be shown in the image view. Kingfisher makes sure images from the exact same url are only downloaded once, it also provides a way to cancel downloading when for example: when the cell that should display the image got scrolled outside the visible area.

Peter Combee
  • 595
  • 4
  • 10