Search code examples
iosswiftsdwebimage

How to push SDWebImage images to a detail view


I'm trying to figure out a way to push images that are loaded via SDWebImage on a TableView, to a view (DetailView) where the image can be viewed in fullscreen.

I have the images loaded from URLs displaying on a table view correctly. But when I tap on one, it segues to another view (DetailView) that is blank when I have a UIImage there. For some reason, the image is not loading.

Thanks!

Here is the TableView code:

import UIKit

class TableViewController: UITableViewController {

var imageURLs = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    imageURLs = ["https://i.imgur.com/lssFB4s.jpg","https://i.imgur.com/bSfVe7l.jpg","https://i.imgur.com/vRhhNFj.jpg"]


    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}

override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "DetailView")  {

        let VC = segue.destinationViewController as! DetailViewController
        if let indexpath = self.tableView.indexPathForSelectedRow {

            let Imageview = imageURLs[indexpath.row] as String
            VC.SentData1 = Imageview
        }

    }



}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    let cell2: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

    let imagename = UIImage(named: imageURLs[indexPath.row])
    cell2.cellImage.image = imagename


    let imageView = cell?.viewWithTag(1) as! UIImageView

    imageView.sd_setImage(with: URL(string: imageURLs[indexPath.row]))

    return cell!


}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return imageURLs.count
}

Here is the DetailView code:

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var Detailimageview: UIImageView!

var SentData1:String!


override func viewDidLoad() {
    super.viewDidLoad()


    Detailimageview.image = UIImage(named: SentData1)


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

Solution

  • You are using UIImage(named:) in your destination VC. This will try and load the image from your bundle, not from the network. You should use sd_setImage to fetch it from the network (or via cache if it has already been fetched):

     Detailimageview.sd_setImage(with: URL(string:self.SentData1))  
    

    Note that properties and variables should start with a lower case letter by convention