Henrique Giuliani Henrique Giuliani - 2 months ago 19
iOS Question

Cannot pass image to another view (after getting async)

I am not being able to pass an

from a
to a
. The image is being displayed in the TableView, but when i try to pass to the other view, it gets

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

And that's how i populate my

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")
loadImageFromUrl(url: catalogOfProducts[indexPath.row].productImageURL!, view: cell.productImageView)

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

is how i pass the
to the respective object.

To pass the data via segue, i just pass the

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)!


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.