我有工作条码扫描器代码,它有自动打开相机与viewDidAppear我想改变它按钮操作打开和关闭我尝试但我不解决它我的代码在下面。 (我还添加了closeCamera和openCamera动作)感谢您的帮助。
@IBOutlet weak var cameraView: DesignableView!
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
self.setupCaptureSession()
}
//MARK: Session Startup
private func setupCaptureSession(){
self.captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do {
let deviceInput = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput
//Add the input feed to the session and start it
self.captureSession.addInput(deviceInput)
self.setupPreviewLayer({
self.captureSession.startRunning()
self.addMetaDataCaptureOutToSession()
})
} catch let setupError as NSError {
self.showError(setupError.localizedDescription)
}
}
private func setupPreviewLayer(completion:() -> ()){
self.captureLayer = AVCaptureVideoPreviewLayer(session: captureSession) as AVCaptureVideoPreviewLayer
if let capLayer = self.captureLayer {
capLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
capLayer.frame = self.cameraView.frame
self.view.layer.addSublayer(capLayer)
completion()
}
else {
self.showError("An error occured beginning video capture.")
}
}
//MARK: Metadata capture
func addMetaDataCaptureOutToSession() {
let metadata = AVCaptureMetadataOutput()
self.captureSession.addOutput(metadata)
metadata.metadataObjectTypes = metadata.availableMetadataObjectTypes
metadata.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
}
//MARK: Delegate Methods
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
for metaData in metadataObjects {
let decodedData:AVMetadataMachineReadableCodeObject = metaData as! AVMetadataMachineReadableCodeObject
self.pCodeTextField.text = decodedData.stringValue
//decodedData.type
}
}
//MARK: Utility Functions
func showError(error:String) {
let alertController = UIAlertController(title: "Error", message: error, preferredStyle: .Alert)
let dismiss:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler:{(alert:UIAlertAction) in
alertController.dismissViewControllerAnimated(true, completion: nil)
})
alertController.addAction(dismiss)
self.presentViewController(alertController, animated: true, completion: nil)
}
@IBAction func closeCamera(sender: AnyObject) {
// HERE I WANT CLOSE CAMERA WHEN CLICKED
}
@IBAction func openCamera(sender: AnyObject) {
// HERE I WANT OPEN CAMERA WHEN CLICKED
}
谢谢。