0

I am grabbing my images from my server, grabbing the URL from my database, and displaying it inside my custom tableview cell.

I'm using the extension below to convert URL into images. As you can see it is fetching the images asynchronously, but it doesn't show the image. I checked the URL is valid, but it's not displaying

After I've fetched my data using NSJSONSerialization, I did a

dispatch_async(dispatch_get_main_queue(), {
                        self.tableView.reloadData()
                    })

This is how I call it in my cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:LatestPostCustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("LatestPostCustomTableViewCell", forIndexPath: indexPath) as! LatestPostCustomTableViewCell

        cell.imagePost.downloadedFrom(link: latestPost[indexPath.row].imageURL, contentMode: .ScaleAspectFit)

        return cell
    }
}

The extension that I'm using retrieved from Loading/Downloading image from URL on Swift

extension UIImageView {
    func downloadedFrom(link link:String, contentMode mode: UIViewContentMode) {
        guard
            let url = NSURL(string: link)
            else {return}
        contentMode = mode
        NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
            guard
                let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
                let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
                let data = data where error == nil,
                let image = UIImage(data: data)
                else { return }
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                self.image = image
            }
        }).resume()
    }
}
Community
  • 1
  • 1
jo3birdtalk
  • 616
  • 5
  • 20
  • 1
    I'm not seeing your question. What's happening? What do you expect to see? It looks like the code you've posted will update the image view. – Dave Batton Feb 07 '16 at 05:31
  • @DaveBatton thanks for replying. My image is not coming up. I'm suspecting it has something to do with refreshing the tableview. Question is how do I do so? – jo3birdtalk Feb 07 '16 at 05:54
  • ^^ `myTableView.reloadData()` ^^ To update `UITableView` But you have already mentioned it - Try checking the reuse of cells – Coldsteel48 Feb 07 '16 at 05:55
  • @user3351949 the flow of my app goes to database and fetch, using json to store string variables, then using dispatch reload tableview. Through the stored variables, I open imageurl, and fetch image again. However in the extension, I reload my image, but not the tableview. I need to know how to reload tableview outside of the class below the self.image = image – jo3birdtalk Feb 07 '16 at 06:05
  • There is plenty of ways to call for reload: 1)Possible way is to send a notification. 2) another is to pass the tableView it self to your extension and invoke `reloadData()` 3) method you can also use delegate pattern. But I doubt you need to Reload the Whole tableView for this – Coldsteel48 Feb 07 '16 at 06:12
  • There's no need to refresh the table view with the extension above. It would probably make things worse. – Dave Batton Feb 07 '16 at 06:12
  • You don't need to reload table view after setting image, problem is with something else – Vishnu gondlekar Feb 07 '16 at 06:12
  • Have you traced through the extension code? The guards in there check for lots of things that might cause the image not to update. Is `url` getting properly set? That can fail. Does it ever reach the line of code where it assigns the image? – Dave Batton Feb 07 '16 at 06:12
  • Actually I did troubleshoot the issue by changing it to a "lower quality" image, and it loads fine presumingly because the asynchronous fetching finishes earlier than the reloading of tableview. – jo3birdtalk Feb 07 '16 at 06:23
  • You guys are right. It is really something with my image. Changing to apple image taken from web its ok. But when from my database url, there is something wrong. Now i don't know where should i solve this problem...... – jo3birdtalk Feb 07 '16 at 07:00
  • the problem is something else of the loading image – Amr Angry Feb 19 '17 at 14:21

0 Answers0