1

我正在尝试构建一个相机应用程序。由于我是 iOS 新手,所以我阅读了文档、观看了教程并编写了以下代码:

import UIKit
import AVFoundation
class StartVC: UIViewController {

var connection: AVCaptureConnection!
var output: AVCapturePhotoOutput!
var videoPreviewLayer: AVCaptureVideoPreviewLayer!
@IBOutlet var videoPreviewView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.createCamera()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.videoPreviewLayer.bounds = self.videoPreviewView.bounds
    self.videoPreviewLayer.position = CGPoint(x: self.videoPreviewView.bounds.midX, y: self.videoPreviewView.bounds.midY)
}

func createCamera() {
    let captureSession = AVCaptureSession()

    if captureSession.canSetSessionPreset(AVCaptureSessionPresetHigh) {
        captureSession.sessionPreset = AVCaptureSessionPresetHigh
    } else {
        print("Error: Couldn't set preset = \(AVCaptureSessionPresetHigh)")
        return
    }

    let cameraDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    var error: NSError?

    let inputDevice = try! AVCaptureDeviceInput.init(device: cameraDevice)
    //let inputDevice = AVCaptureDeviceInput.withDevice(cameraDevice) as AVCaptureDeviceInput
    if let error = error {
        print("Error: \(error)")
        return
    }

    if captureSession.canAddInput(inputDevice) {
        captureSession.addInput(inputDevice)
    } else {
        print("Error: Couldn't add input device")
        return
    }

    let imageOutput = AVCapturePhotoOutput()
    if captureSession.canAddOutput(imageOutput) {
        captureSession.addOutput(imageOutput)
    } else {
        print("Error: Couldn't add output")
        return
    }

    // Store imageOutput. We will need it to take photo
    self.output = imageOutput

    let connection = imageOutput.connections.first as! AVCaptureConnection!
    // Store this connection in property. We will need it when we take image.
    self.connection = connection
    connection?.videoOrientation = AVCaptureVideoOrientation.portrait

    captureSession.startRunning()

    // This will preview the camera
    let videoLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
    videoLayer?.contentsScale = UIScreen.main.scale

    self.videoPreviewView.layer.addSublayer(videoLayer!)

    // Store this layer instance in property. We will place it into a view
    self.videoPreviewLayer = videoLayer
}



@IBAction func CaptureButton(_ sender: UIButton) {

    let captureSettings = AVCapturePhotoSettings()
    let previewPixelType = captureSettings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                         kCVPixelBufferWidthKey as String: 300,
                         kCVPixelBufferHeightKey as String: 300,
                         ]
    captureSettings.previewPhotoFormat = previewFormat

    self.output.capturePhoto(with: captureSettings, delegate: self as! AVCapturePhotoCaptureDelegate)

}

}

但是我在本节中遇到了错误。

@IBAction func CaptureButton(_ sender: UIButton) {

    let captureSettings = AVCapturePhotoSettings()
    let previewPixelType = captureSettings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                         kCVPixelBufferWidthKey as String: 300,
                         kCVPixelBufferHeightKey as String: 300,
                         ]
    captureSettings.previewPhotoFormat = previewFormat

// For this line
        self.output.capturePhoto(with: captureSettings, delegate: self as! AVCapturePhotoCaptureDelegate)

    }

错误是说它无法将myViewController的值 转换为AVCapturePhotoCaptureDelegate

我怎么解决这个问题?

4

3 回答 3

1

此错误是因为您没有将 AVCapturePhotoCaptureDelegate 确认到您编写此代码的 viewController。

要解决此错误,请在您的类声明中添加“AVCapturePhotoCaptureDelegate”,如下所示:

class myViewController: UIViewController,AVCapturePhotoCaptureDelegate{


}
于 2017-02-23T11:20:32.640 回答
0

试试这个

class StartVC: UIViewController {
}
extension StartVC: AVCapturePhotoCaptureDelegate{
}

不需要铸造

self.output.capturePhoto(with: captureSettings, delegate: self)

也可以写成

class StartVC: UIViewController,AVCapturePhotoCaptureDelegate {


}
于 2017-02-23T11:33:24.380 回答
0

尝试在 info.plist 中添加“隐私 - 相机使用说明”键。如果您使用的是 iOS 10+。

从我们的应用程序访问相机需要此密钥。

于 2017-02-23T12:12:29.483 回答