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]
}
}