使用自定义相机 Swift 3 拍照 [英] Taking photo with custom camera Swift 3

查看:29
本文介绍了使用自定义相机 Swift 3 拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 2.3 中,我使用此代码在自定义相机中拍照:

in Swift 2.3 I used this code to take a picture in custom camera:

 func didPressTakePhoto(){

        if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) {

            stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
                if sampleBuffer != nil {
                    let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    let dataProvider = CGDataProviderCreateWithCFData(imageData)
                    let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
                    let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)


                    self.captureImageView.image = image
                }
            })

    }
}

但是他的一行:stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in

显示此错误:

AVCapturePhotoOutput"类型的值没有成员'captureStillImageAsynchronouslyFromConnection'

Value of type 'AVCapturePhotoOutput' has no member 'captureStillImageAsynchronouslyFromConnection'

我尝试解决我的问题,但我总是遇到越来越多的错误,所以我发布了我的原始代码.

I tried solving my problem but I always get more and more errors so that is why I post my original code.

有人知道如何让我的代码再次运行吗?

Does anybody know how to make my code work again?

谢谢.

推荐答案

感谢 Sharpkits 我找到了我的解决方案(此代码对我有用):

Thanks to Sharpkits I found my solution (This code works for me):

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

        if let error = error {
            print(error.localizedDescription)
        }

        if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer,
            let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: nil)
            let dataProvider = CGDataProvider(data: imageData as! CFData)

            let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)


            let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)

            let cropedImage = self.cropToSquare(image: image)

            let newImage = self.scaleImageWith(cropedImage, and: CGSize(width: 600, height: 600))

            print(UIScreen.main.bounds.width)


            self.tempImageView.image = newImage
            self.tempImageView.isHidden = false


        } else {

        }
    }

这篇关于使用自定义相机 Swift 3 拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆