This is my code. When I click the captureButton it executes without error. The purpose is to save a CIImage in captureProcessor.image
import UIKit
import AVFoundation
class ViewController: UIViewController {
let captureProcessor = captureProcess()
let session = AVCaptureSession()
let capturedPhotoOutPut = AVCapturePhotoOutput()
@IBOutlet weak var previewOfCamera: UIView!
@IBOutlet weak var imageShow: UIImageView!
@IBAction func captureButton(_ sender: Any) {
captureProcessor.capturePhoto(capturedPhotoOutPut)
// getCGRectInfo()
// code above is to get CGRect info from captureProcessor.image
}
override func viewDidLoad() {
super.viewDidLoad()
configurePreview()
}
func configureTheSession () -> Void {
var videoInput:AVCaptureDeviceInput!
let videoDevice = AVCaptureDevice.DiscoverySession(deviceType:[.builtInWideAngleCamera], mediaType: .video, position: .back).devices.first
videoInput = try! AVCaptureDeviceInput(device: videoDevice!)
session.beginConfiguration()
session.sessionPreset = .hd1280x720
session.addInput(videoInput)
session.addOutput(capturedPhotoOutPut)
session.commitConfiguration()
DispatchQueue.global(qos: .userInitiated).async {
self.session.startRunning()
}
}
And here is another class mentioned above
import Foundation
import AVFoundation
class captureProcess: NSObject, AVCapturePhotoCaptureDelegate{
var image:CIImage!
func capturePhoto (_ photoOutput: AVCapturePhotoOutput) {
let settings = AVCapturePhotoSettings()
settings.flashMode = .off
photoOutput.capturePhoto(with: settings, delegate: self)
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
image = CIImage(data: imageData!)
}
But if I add an action in @IBAction, e.g. getInfo (comment in first chunk of code). Program will omit capturePhoto (it runs this line but doesn't execute the delegate) but executes getCGRectInfo first. The image variable is still empty. So the program will crash.
Is there any way to execute delegate method once the program run to capturePhoto line so that I can get the CIImage info properly