1

I'm trying to load image(s) from remote url, after the image has been loaded, I assign it to UIImageView on a custom cell inside UITableViewController:

func loadImage(url: NSURL) {
  self.myImageView?.contentMode = UIViewContentMode.ScaleAspectFit
  let queue = dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)

  dispatch_async(queue) {
    if let imageData = NSData(contentsOfURL: url) {
      dispatch_async(dispatch_get_main_queue()) {
        self.myImageView?.image = UIImage(data: imageData)
      }
    }
  }
} 

It runs good, except that myImageView does not load the image (I know it's asynchronously loading the image from the remote location), but if I click the cell or rotate the iphone simulator, it shows the image. Any idea why this happens?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
sendy halim
  • 405
  • 4
  • 11
  • Add `self.tableView.reloadData()` after this: `self.myImageView?.image = UIImage(data: imageData)` – Islam Jul 17 '15 at 19:09
  • A side note: Are you making URL connections? It doesn't seem like it. If not I don't think you need the `dispatch_async`. It shouldn't really matter for your problem but just in case. – somtingwong Jul 17 '15 at 19:15
  • When are you calling the loadImage function? – Andrew M Jul 17 '15 at 20:08
  • Sounds the same as http://stackoverflow.com/a/12209846/629118 setNeedsLayout – Kazuki Sakamoto Jul 17 '15 at 20:37
  • @somtingwong Yes I'm making url connections to get the image(s) @AndrewM I'm calling the loadImage function immediately inside `func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell` using `let cell = tableView.dequeueReusableCellWithIdentifier("my-cell", forIndexPath: indexPath) as! MyCustomCell` `MyCustomCell` just have `loadImage` method – sendy halim Jul 18 '15 at 03:00

0 Answers0