1

I use the WebServices(SOAP) to reach the data. I take the image urls from database , download them and show in TableView. here is my codes.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "gorevlerCell",for: indexPath) as! GorevlerCell
    cell.contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)

    // Fil the gorevler Cell

    cell.txtGorevAdi.text = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Gorev_Adi") as? String
    cell.txtGoreviAlan.text = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Gorevi_Alan") as? String
    cell.txtGoreviVeren.text = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Gorevi_Veren") as? String
    cell.txtTarih.text = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Tarih") as? String
    cell.txtOncelikDurumu.text = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Oncelik_Durumu") as? String


    // The Image File
    let imgUrl2 = (gorevler.object(at: indexPath.row) as AnyObject).value(forKey: "Profil_Url") as? String
    let trimmedImgUrl = imgUrl2?.trimmingCharacters(in: .whitespaces)
    let url = NSURL( string : "http://"+trimmedImgUrl! )
    let data = NSData(contentsOf: url! as URL)
    let img = UIImage(data: data! as Data)
    cell.profileImage.image = img
    cell.profileImage.layer.cornerRadius = 35
    cell.profileImage.layer.borderWidth = 2.0
    cell.profileImage.layer.borderColor = UIColor.lightgray.cgColor
    cell.profileImage.clipsToBounds = true

    return cell}

So, When i added the image in the TableView and scrolled it , the application began slow down. My question is that why the application is slow down and how to faster it.

Note : i can try with low resolution image but still slow.

Mevlüt Soner
  • 161
  • 3
  • 4
  • 14
  • how ur downloading images using any 3rd party library ? – Uma Madhavi Dec 13 '17 at 07:41
  • Looks images are downloaded when tableview is loaded.. which means when we scroll many images are loaded from URL. i recommend to change the design, so images are downloaded in background separately from table view and table view in notified about new images downloaded. – mkumar Dec 13 '17 at 07:45

1 Answers1

2

Its slow because you are download image Synchronously.

You need to download it Asynchronously. Consider below example:

Add below extension in your app.

extension UIImageView {
    func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
            else { return }
            DispatchQueue.main.async() { 
                self.image = image
            }
        }.resume()
    }
    func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloadedFrom(url: url, contentMode: mode)
    }
}

And you can use it in your case:

cell.profileImage.downloadedFrom(link: "http://"+trimmedImgUrl!)

Replace above line with:

let url = NSURL( string : "http://"+trimmedImgUrl! )
let data = NSData(contentsOf: url! as URL)
let img = UIImage(data: data! as Data)
cell.profileImage.image = img

For more info check original post.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165