2

I am not being able to pass an UIImageView from a tableView to a UIViewController. The image is being displayed in the TableView, but when i try to pass to the other view, it gets nil

The method to download the image is:

 func loadImageFromUrl(url: String, view: UIImageView){

      // Create Url from string
      let url = NSURL(string: url)!

      // Download task:
      // - sharedSession = global NSURLCache, NSHTTPCookieStorage and NSURLCredentialStorage objects.
      let task = URLSession.shared.dataTask(with: url as URL) { (responseData, responseUrl, error) -> Void in
           // if responseData is not null...
           if let data = responseData{

                // execute in UI thread
                DispatchQueue.main.async {
                     view.image = UIImage(data: data)
                }
           }
      }

      // Run task
      task.resume()
 }

And that's how i populate my tableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Bundle.main.loadNibNamed("UIProductTableViewCell", owner: self, options: nil)?.first as! UIProductTableViewCell
    cell.selectionStyle = .none

    //Populating data
    cell.descriptionLabel.text = catalogOfProducts[indexPath.row].name
    cell.priceLabel.text = catalogOfProducts[indexPath.row].regularPrice

    if (catalogOfProducts[indexPath.row].productImageURL == "") {
       cell.productImageView.image = UIImage(named: "noImage")
    }else{
      loadImageFromUrl(url: catalogOfProducts[indexPath.row].productImageURL!, view: cell.productImageView)

    }
    catalogOfProducts[indexPath.row].productImage = cell.productImageView.image
    return cell
}

catalogOfProducts[indexPath.row] is how i pass the UIImage to the respective object.

To pass the data via segue, i just pass the Product object:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "productDetailSegue", sender: catalogOfProducts[indexPath.row])
}

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let productDetailViewController = segue.destination as! ProductDetailViewController
      productDetailViewController.featuredProduct = (sender as? Product)!
 }
  • I suggest you use something like SDWebImage. It provide caching so you can just download the image again in the second view controller and it will come from cache if it has already been downloaded – Paulw11 Nov 07 '16 at 07:19
  • first of all its wrong way to store all image in array and pass particular image to another view you have to use path where its downloaded. – CodeChanger Nov 07 '16 at 07:20
  • thanks for your comment! Actually the image is being stored in a property of a `Product` . The array is just storing `Product` types – Henrique Giuliani Nov 07 '16 at 07:25
  • did you actually checked if your sender in your **prepare for segue** function is actually a Product object? I'm not sure it is ;) – RomOne Nov 07 '16 at 07:25
  • for easy to manage all product images you can check my answer. – CodeChanger Nov 07 '16 at 07:27

3 Answers3

2

For simply manage downloaded images you can use SDWebImage library.

Link of SDWebImage : https://github.com/rs/SDWebImage

This Library nicely manage images with path.

How to Use SDWebImage :

cell.productImageView.sd_setImageWithURL(NSURL(string: catalogOfProducts[indexPath.row].productImageURL!), placeholderImage:UIImage(imageNamed:"placeholder.png"))

while just pass your image URL to next view and agin use same code to get image in next view.

Note : it will not download image agin and return that downloaded image while you call above function agin.

Hope this will help.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
1

Regarding this -

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let productDetailViewController = segue.destination as! ProductDetailViewController productDetailViewController.featuredProduct = (sender as? Product)! }

Check what is in your sender here? You can just send the image name then you can access it to next view controller.

User511
  • 1,456
  • 10
  • 28
0

you're not able to pass views in iOS, how I would deal with this in my apps is just pass the name of the image and reload it in the new viewcontroller

yawnobleix
  • 1,204
  • 10
  • 21
  • Can you please share with me how do you store the image and pass just the name? – Henrique Giuliani Nov 07 '16 at 08:08
  • sorry in my app I always have access to the image name, but if you don't have that then you should be passing the image data. Unfortunately I am not so familiar with swift but here is how you convert UIImage -> NSData and then NSData -> UIImage Here is an example in Objective-C http://stackoverflow.com/questions/20013144/convert-uiimage-to-nsdata-and-back-to-uiimage – yawnobleix Nov 07 '16 at 08:16