1

Having a problem with this code. Basically i'm trying to populate a table cell using an image im pulling from twitter. The url field here has the value http://pbs.twimg.com/profile_images/796924570150301696/35nSG5nN_normal.jpg but for some reason the print("REACHED") is never printed. Any help/suggestions appreciated!

code snippet:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tIdentifier, for: indexPath) as! TweetCell
    let tweet = tweets[indexPath.section][indexPath.row]
    let url = tweet.user.profileImageURL!
    print(url.absoluteString)
    let data = try? Data(contentsOf: url)
    if (data == nil){
    } else {
        print("REACHED")
        cell.avatarImage = UIImage(data: data!)
    }
    cell.tweet = tweets[indexPath.section][indexPath.row]
    return cell
}

2 Answers2

2

This worked for me:

func example() {
    let cell = UITableViewCell()
    let url = URL(string: "http://pbs.twimg.com/profile_images/796924570150301696/35nSG5nN_normal.jpg")

    do {
        let data = try Data(contentsOf: url!)
        print("REACHED")
        cell.imageView?.image = UIImage(data: data)
    } catch {
        print("received this error:\n\(error.localizedDescription)")
    }
}

If it doesn't work right away, at least you'll have an error message to help you figure it out. Good luck!

Edit: You should make sure you have updated your Info.plist to include an entry for:

App Transport Security Settings

Without this you will not have access to other sites. Transport security has blocked a cleartext HTTP

Community
  • 1
  • 1
Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • I don't see a path to the file. Add a breakpoint in the method above where you load the file. Step through and verify as you go. – Mozahler Mar 24 '17 at 11:53
  • Awesome! Glad I could help! – Mozahler Mar 24 '17 at 11:56
  • This originally gave me a "cannot open file error" which lead me to re-examine my plist source code and by adding `NSAppTransportSecurity NSAllowsArbitraryLoads ` I was able to get it running. Thanks @Mozahler – James Dorrian Mar 24 '17 at 11:56
  • Funny, that was going to be my next suggestion. I was about to copy and paste from my Info.plist. I quickly tested the code in an app that had NSAPPTransportSecurity set. – Mozahler Mar 24 '17 at 11:57
0

Some tips for an easy life…

  • Don't force unwrap
  • Don't download on the main queue
  • Don't expose your cell's IBOutlets

let imageQueue = DispatchQueue(label: "imageQueue", qos: DispatchQoS.background)

class TweetCell: UITableViewCell {

    @IBOutlet fileprivate var avatarImage: UIImageView!

    var tweet: Tweet {        
        didSet {
            guard let url = tweet.user.profileImageURL else { return }

            loadImage(url: url)
        }
    }

    fileprivate func loadImage(url: URL) {
        imageQueue.async {

            do {
                let data = try Data(contentsOf: url) 

                DispatchQueue.main.async {
                    self.avatarImage.image = UIImage(data: data)        
                }

            } catch {
                // Handle error
            }    
        }
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tIdentifier, for: indexPath) as! TweetCell
    cell.tweet = tweets[indexPath.section][indexPath.row]
    return cell
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160