首页 > 解决方案 > Swift AV 基金会

问题描述

这实际上是他们标记的两个问题 //******************************* 我把整个班级都放在了这个帖子中,

第一个是

//====================================
//====================================
    @IBAction func btnMakeReport(_ sender: Any) {

        let settings = AVCapturePhotoSettings()
        //****************    self is erroring... getting...
        // /Users/vyoumans/Documents/vyDEVELOPMENT/iOS/TESTS/camtest11/camtest11/VCCam02a.swift:104:61: Argument type 'VCCam02a' does not conform to expected type 'AVCapturePhotoCaptureDelegate'

        photoOutput?.capturePhoto(with: settings, delegate: self)

我能否得到一些关于为什么 self 不会向 AVCapturePhotoCaptureDelagate 确认的建议。

第二个... //******************图像是错误的...但我在上面定义了图像。

我在课程开始时声明图像。//===================================== extension ViewController: AVCapturePhotoCaptureDelegate { 在应用程序底部出现此错误

谢谢

//-------- 代码开始。------------

'
import UIKit
import AVFoundation

//====================================

class VCCam02a: UIViewController {
    var captureSession = AVCaptureSession()
    var backCamera: AVCaptureDevice?
    var frontCamera: AVCaptureDevice?
    var currentCamrera: AVCaptureDevice?

    var photoOutput: AVCapturePhotoOutput?

    var cameraPreviewLayer: AVCaptureVideoPreviewLayer?

//    var image: UIImage?
    var image: UIImage?



    override func viewDidLoad() {
        super.viewDidLoad()

        setupCaptureSession()
        setupDevice()
        setupInputOutput()
        setupPreviewLayer()
        startRunningCaptureSession()


    }
//====================================
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//====================================
//====================================
    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
            }
        }

        currentCamrera = backCamera
    }



//====================================
    func setupInputOutput() {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamrera!)
            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 btnMakeReport(_ sender: Any) {

        let settings = AVCapturePhotoSettings()
//****************    self is erroring... getting...
// /Users/vyoumans/Documents/vyDEVELOPMENT/iOS/TESTS/camtest11/camtest11/VCCam02a.swift:104:61: Argument type 'VCCam02a' does not conform to expected type 'AVCapturePhotoCaptureDelegate'

        photoOutput?.capturePhoto(with: settings, delegate: self)
        // performSegue(withIdentifier: "showPhoto_Segue", sender: nil)
    }
//====================================
    @IBAction func btnCancel(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }


//====================================
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showPhoto_Segue" {
            let VCPreviewViewController = segue.destination as! VCPreviewViewController
            VCPreviewViewController.image = self.image
        }
    }
//====================================
//====================================
//====================================
//====================================
//====================================
//====================================
//====================================
//====================================
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
//====================================
extension ViewController: AVCapturePhotoCaptureDelegate {
    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        if let imageData = photo.fileDataRepresentation() {

   //**************   image is error...  but I define image above.
            ///Users/vyoumans/Documents/vyDEVELOPMENT/iOS/TESTS/camtest11/camtest11/VCCam02a.swift:143:13: Use of unresolved identifier 'image'

            image = UIImage(data: imageData)
            performSegue(withIdentifier: "showPhoto_Segue", sender: nil)
        }
    }
}


//====================================
//====================================
//====================================
//====================================
//====================================


'

标签: swiftavfoundation

解决方案


好的,这一行:

photoOutput?.capturePhoto(with: settings, delegate: self)

似乎是问题所在。听起来您遇到了一个 self 不符合AVCapturePhotoCaptureDelegate协议的编译器错误。

这很不言自明。如果您的类符合AVCapturePhotoCaptureDelegate协议,则需要将该符合性添加到类定义或扩展中:

class VCCam02a: UIViewController, AVCapturePhotoCaptureDelegate

然后,您将需要在该协议中实现处理您正在执行的捕获类型所需的所有方法。引用文档:

此协议中的所有方法在编译时都是可选的,但在运行时,您的委托对象必须根据您的照片设置响应某些方法:

(有关更多信息,请参阅有关协议的 Apple 文档。)


推荐阅读