UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.