【问题标题】:AVCapturePhotoOutput has no member 'captureStillImageAsynchronously'AVCapturePhotoOutput 没有成员“captureStillImageAsynchronously”
【发布时间】:2017-03-24 21:38:05
【问题描述】:

我正在制作一个自定义相机应用程序,我可以在其中通过 takePhoto 按钮拍摄我的照片,但 takePhoto 中出现错误,提示 AVCapturePhotoOutput 没有成员“captureStillImageAsynchronously”。

视图控制器

  import UIKit
  import AVFoundation
  import CoreImage

 @available(iOS 10.0, *)
 class ViewController: UIViewController {

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var previewLayer = AVCaptureVideoPreviewLayer()


@IBOutlet weak var imageViewReal: UIImageView!
@IBOutlet weak var cameraView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

}



override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

   let deviceSession = AVCaptureDeviceDiscoverySession.init(deviceTypes: [.builtInDuoCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)

    for device in (deviceSession?.devices)! {


        if (device as AnyObject).position == AVCaptureDevicePosition.front {
            do {
                let input = try AVCaptureDeviceInput(device: device )

                if captureSession.canAddInput(input) {
                   captureSession.addInput(input)

                    if captureSession.canAddOutput(sessionOutput){
                    captureSession.addOutput(sessionOutput)

                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer.connection.videoOrientation = .portrait

                        cameraView.layer.addSublayer(previewLayer)
                        cameraView.addSubview(takePhoto)

                        previewLayer.position = CGPoint (x: self.imageViewReal.frame.width / 2, y: self.imageViewReal.frame.height / 2 )
                        previewLayer.bounds = imageViewReal.frame
                        captureSession.startRunning()

                    }
                }
            }
            catch  {
            print("Error")
            }
        }
    }
}


@IBAction func takePhoto(_ sender: Any) {
    if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {
        sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
        buffer, Error in
            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)

            UIImageWriteToSavedPhotosAlbum(UIImage(data : imageData!)!, nil, nil, nil)
        })
    }
}
}

【问题讨论】:

    标签: ios swift xcode camera


    【解决方案1】:

    如果你的应用兼容iOS9,请使用AVCaptureStillImageOutput的方法:

    func captureStillImageAsynchronously(from connection: AVCaptureConnection!, 
                       completionHandler handler: ((CMSampleBuffer?, Error?) -> Void)!)
    

    如果你的应用从iOS10开始,你应该使用AVCapturePhotoOutput的方法:

    open func capturePhoto(with settings: AVCapturePhotoSettings, delegate: AVCapturePhotoCaptureDelegate)
    

    所以,var sessionOutput = AVCapturePhotoOutput() 应该是 var sessionOutput = AVCaptureStillImageOutput()

    试一试。

    【讨论】:

    • 感谢您的努力,但我在第 54 行也遇到错误 cameraView.addSubview(takePhoto) errorr: cannot convert the value of type Any to UIview
    • 为您的努力绘制thanx,但我在第54行也遇到错误 cameraView.addSubview(takePhoto) errorr: cannot convert the value of type Any to UIview
    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多