gpt4 book ai didi

swift - 无法使用自定义相机拍摄照片

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:17 25 4
gpt4 key购买 nike

我正在尝试显示从我的自定义相机拍摄的图像(来自类似 snapchat 的菜单,您可以在其中向右滑动以打开左侧的相机)。我将代码更新为 3.0,但现在它给了我这个错误:“AVCapturePhotoOutput”没有成员“captureStillImageAsynchronouslyFromConnection”,我找不到修复它的方法。 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(动画)

    captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSession.Preset.hd1920x1080

let backCamera = AVCaptureDevice.default(for: AVMediaType.video)

do {
let input = try AVCaptureDeviceInput(device: backCamera!)

if (captureSession?.canAddInput(input) != nil) {
captureSession?.addInput(input)

stillImageOutput = AVCapturePhotoOutput()

captureSession?.addOutput(stillImageOutput!)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}

} catch {
print("Something is wrong with the camera")
}

}
func didPressTakePhoto() {
if let videoConnection = stillImageOutput?.connection(with: AVMediaType.video) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
(sampleBuffer, error) in

if sampleBuffer != nil {
var imageData = AVCaptureStillImageOutput.jsegStillImageNSDataRepresentation(sampleBuffer)
var dataProfider = CGDataProviderCreateWithCFData(imageData)
var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

var image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)

self.tempImageView.image = image
self.tempImageView.isHidden = true
}


})
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
didPressTakePhoto()
}

我注意到 stillImageOutput 已被弃用,现在改用 AVCapturePhotoOutput。在 3.0 中(使用 AVCapturePhotoOutput)编写 captureStillImageAsynchronously 的正确方法是什么?

我看到了这个问题的其他答案,但没有一个对我有用。我想要的只是在预览照片时点击(操作)拍摄照片(我可以接受它并在接受后执行其他逻辑)。

最佳答案

let photoSettings = AVCapturePhotoSettings()
photoSettings.isAutoStillImageStabilizationEnabled = true
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = .auto
stillImageOutput?.capturePhoto(with: photoSettings, delegate: self)

extension YourViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, ..., error: Error?) {
guard error == nil, let photoSampleBuffer = photoSampleBuffer else { return }
guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { return }
let capturedImage = UIImage(data: imageData, scale: 1.0)
...
}
}

手写,如有错别字请见谅

关于swift - 无法使用自定义相机拍摄照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com