-1

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
    })
}
Duncan
  • 73
  • 1
  • 8

1 Answers1

1

Create var imageFromWebController: UIImage? in your ViewPhotoController.swift

then pass your image from WebController.swift like the below code

if segue.identifier == "viewLargePhoto" {
        var viewPhotoController = segue.destination as! ViewPhotoController
        //Line beneath is the trouble causer
        viewPhotoController.imageFromWebController = sendImage
    }

Assign that image to your imageView in ViewPhotoController

override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = imageFromWebController
    }
Naren Krish
  • 259
  • 2
  • 14
  • Thank you for offering a solution. Unfortunately it does not pass the image to the ViewPhotoController. I know that the button is performing some actions as it takes a screenshot image as I can see the image if I look in the PhotoLibrary, and it segues to the ViewPhotoController. When I am segued to the ViewPhotoController, the UIImageView is empty. – Duncan Apr 13 '18 at 21:32
  • This is the error that I am recieving : HuntingLicense[69913:3549111] >'s window is not equal to 's view's window! – Duncan Apr 13 '18 at 21:33
  • @Duncan check whether you are using 2 segues, you can use one or probably It will be a auto layout error go to the storyboard and resolve the constraint error – Naren Krish Apr 16 '18 at 07:27
  • I did have an extra line of code in my button function that calling to another segue. I took it out and did not recieve any errors but I am still in the same boat on being presented the new view controller without the image. I can still see that my phone is capturing an image when the button is clicked but I cannot figure out how to present that picture in the view controller the button is segueing to. – Duncan Apr 16 '18 at 21:03