Home > database >  Error when trying to segue from one view controller to another
Error when trying to segue from one view controller to another

Time:11-22

I am trying to preform a segue between my home view controller to my Preview view controller that will actually show the taken photos. I have named this segue in the storyboard as "showPhoto". but when i'm trying to actually segue from home view Controller to preview View Controller I am getting a few errors. The first says "Value of type 'HomeViewController' has no member 'image'" However, my preview viewController is where this image has been linked so I am unsure why Xcode is saying that my HomeView Controller has no member image when it should be my preview View Controller that is being segue to. The 2nd error again says that "Cannot find 'image' in scope" My code is below for both Home View Controller and Preview View Controller.

import UIKit
import AVFoundation

class HomeViewController: UIViewController, AVCapturePhotoCaptureDelegate {
    
    
    var captureSession =  AVCaptureSession()
    var backCamera: AVCaptureDevice?
    var frontCamera:  AVCaptureDevice?
    var currentCamera: AVCaptureDevice?
    var photoOutput:  AVCapturePhotoOutput?
    var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
    
    @IBOutlet weak var camerabtn: UIButton!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCaptureSession()
        setupDevice()
        setupInputOutput()
        setupPreviewLayer()
        startRunningCaptureSession()
        
    }
    
    func setupCaptureSession() {
        
        captureSession.sessionPreset =  AVCaptureSession.Preset.photo
    }
    
    func setupDevice() {
        let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
        let devices = deviceDiscoverySession.devices
        
        for device in devices {
            if device.position == AVCaptureDevice.Position.back {
                backCamera = device
            } else if device.position == AVCaptureDevice.Position.front {
                frontCamera = device
            }
        }
        
        currentCamera = backCamera
    }
    
    func setupInputOutput() {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
            captureSession.addInput(captureDeviceInput)
            photoOutput = AVCapturePhotoOutput()
            photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
            captureSession.addOutput(photoOutput!)
        } catch {
            print(error)
        }
    }
    
    func setupPreviewLayer() {
        cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        cameraPreviewLayer?.frame = self.view.frame
        self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
    }
    func startRunningCaptureSession() {
        captureSession.startRunning()
    }
    
    @IBAction func camerabtn(_ sender: Any) {
        let settings = AVCapturePhotoSettings()
        photoOutput?.capturePhoto(with: settings, delegate: self)
        performSegue(withIdentifier: "showPhoto", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showPhoto " {
            let previewVC = segue.destination as! PreviewViewController
            previewVC.image = self.image
        }
    }
    
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        if let imageData = photo.fileDataRepresentation() {
            image = UIImage(data: imageData)
            performSegue(withIdentifier: "showPhoto", sender: nil)
        }
    }
}
    





import UIKit

class PreviewViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    @IBOutlet weak var image : UIImageView!
    
    @IBAction func cancelbtn(_ sender: Any) {
    }
    
    
    @IBAction func savebtn(_ sender: Any) {
    }
}

CodePudding user response:

You are trying to pass UIImage to UIImageView. it s no accessible. In your PreviewViewController you should create an object for UIImage

var newImage: UIImage!

in HomeViewController

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showPhoto " {
        let previewVC = segue.destination as! PreviewViewController
        previewVC.newImage = self.image
    }
}

CodePudding user response:

@IBOutlet weak var image : UIImageView!

this image property is not accessible, so create new property of image like this.

var newImage: UIImage!
  • Related