I am trying to make an app that loads a UIWebView in "WebController" and below the UIWebView is a button that takes a screenshot of the UIWebView and saves it to my PhotoLibrary. I want to send the photo that was just taken when the button is clicked to a new controller, "ViewImageController", and insert into a UIImageView that is in ViewImageController to edit or do some other stuff. I am trying to use a segue inside my WebController to send the image to my ViewImageController. I have seen a lot of articles for passing images between view controllers, but I have only found that they deal with UIImagePickerController delegates which I am not using since I am trying to send a photo just taken instead of taking the photo, then choosing that image from an ImagePicker to send. Please let me know if this is not clear so I can try to explain it better.
WebController.swift
var sendImage:UIImage!
....
....
....
@IBAction func takePhotoBtn(_ sender: UIButton) {
print("Screenshot")
// = UIImage
UIGraphicsBeginImageContext(webPageView.frame.size)
if let aContext = UIGraphicsGetCurrentContext(){
webPageView.layer.render(in: aContext)
}
var viewImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(viewImage!, nil, nil, nil)
viewImage = sendImage
self.performSegue(withIdentifier: "viewLargePhoto", sender: AnyObject.self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let viewImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if segue.identifier == "viewLargePhoto" {
var viewPhotoController = segue.destination as! ViewPhotoController
//Line beneath is the trouble causer
viewPhotoController.imageView = sendImage//Cannot assign value of type 'UIImage!' to type 'UIImageView!'
}
}
ViewPhotoController.swift
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.hidesBarsOnTap = true
self.displayPhoto()
}
....
....
....
func displayPhoto(){
let screenSize: CGSize = UIScreen.main.bounds.size
let targetSize = CGSize(width: screenSize.width, height: screenSize.height)
let imageManager = PHImageManager.default()
imageManager.requestImage(for: self.photoAsset[self.index] as! PHAsset, targetSize: targetSize, contentMode: .aspectFit, options: nil, resultHandler: {(result,info)->Void in
self.imageView.image = result
})
}