BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 50 - Milestone Project: 10-12

Forums > 100 Days of Swift

Hi everyone, I am facing a challenge displaying images into the InterestViewController( the view controller that's supposed to display the image when a row in the UITableView is tapped). The images have a generic name at first and they display well in the table view. However, when tapped, the InterestView opens but does not display any image. Kindly help me sort this out, thank you in advance :)

If you wish to help me sort out this issue, here is the GitHub link to my repository: https://github.com/leonardsangoroh/PicAndCap/tree/displaySelectedImage

Be careful to select "displaySelectedImage" branch and not the main branch.

Help of any form would highly be appreciated. Thank you.

Kind regards,

2      

In your InterestViewController, you're passing var selectedImage: String?. But this is just a string, and then you're trying to get the image with that

if let imageToLoad = selectedImage {
            imageView.image = UIImage(named: imageToLoad)
        }

But you don't have such image in your assets catalogue, so you cannot retrieve it like that.

As you saved image in ViewControlle as

let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)

        ///convert UIImage to Data object so it can be saved
        if let jpegData = image.jpegData(compressionQuality: 0.8) {
            ///write image to disk
            try? jpegData.write(to: imagePath)
        }

so you'll have to get it back from that directory the same way.

Something like that should do the job:

class InterestViewController: UIViewController {

  @IBOutlet weak var imageView: UIImageView!
  var selectedImage: String?

  override func viewDidLoad() {
    super.viewDidLoad()

    if let image = selectedImage {
      let path = getDocumentsDirectory().appendingPathComponent(image)
      imageView.image = UIImage(contentsOfFile: path.path)
    }
  }

  func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
  }

}

2      

Thank you for the assistance, you have made me look at the problem from a different angle and the issue is solved. I made the changes you suggested then made one more change to line :59 of ViewController:

from:

 vc.selectedImage = interest

to:

vc.selectedImage = interest.image

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.