gpt4 book ai didi

ios - 在 iOS10 中使用 AVCapturePhotoOutput - NSGenericException

转载 作者:行者123 更新时间:2023-11-29 05:32:17 24 4
gpt4 key购买 nike

我目前正在尝试弄清楚如何使用 iOS 10 的 AVCapturePhotoOutput 方法,但遇到了困难。我觉得我即将做对,但仍然收到错误:

Terminating app due to uncaught exception 'NSGenericException', reason: '-[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] No active and enabled video connection'

我尝试将这行代码放入 AVCapturePhotoCaptureDelegate 或我的 didPressTakePhoto 函数中:

if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait;
...
}

这是我目前的代码:

import AVFoundation
import UIKit

class Camera: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, AVCapturePhotoCaptureDelegate {

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

var captureSession : AVCaptureSession?
var stillImageOutput : AVCapturePhotoOutput?
var stillImageOutputSettings : AVCapturePhotoSettings?
var previewLayer : AVCaptureVideoPreviewLayer?

var didTakePhoto = Bool();

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

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

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated);

previewLayer?.frame = cameraView.bounds;
}

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

captureSession = AVCaptureSession();
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080;

stillImageOutput = AVCapturePhotoOutput();

let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo);

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

if (captureSession?.canAddInput(input))! {
captureSession?.addInput(input);

if (captureSession?.canAddOutput(stillImageOutput))! {
captureSession?.canAddOutput(stillImageOutput);

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect;
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait;
cameraView.layer.addSublayer(previewLayer!);
captureSession?.startRunning();
}
}
} catch {
print(error);
}
}

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) {
print(UIImage(data: dataImage)?.size as Any);

let dataProvider = CGDataProvider(data: dataImage as CFData);
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent);
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right);

self.imageView.image = image;
self.imageView.isHidden = false;
}
}

func didPressTakePhoto() {
stillImageOutputSettings = AVCapturePhotoSettings();

let previewPixelType = stillImageOutputSettings?.availablePreviewPhotoPixelFormatTypes.first!;
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String: 160];
stillImageOutputSettings?.previewPhotoFormat = previewFormat;

stillImageOutput.capturePhoto(with: stillImageOutputSettings!, delegate: self);
}

func didPressTakeAnother() {
if (didTakePhoto == true) {
imageView.isHidden = true;
didTakePhoto = false;
} else {
captureSession?.startRunning();
didTakePhoto = true;
didPressTakePhoto();
}
}

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

有什么建议吗?

提前致谢!

最佳答案

代码错误。

enter image description here

应该是

if (captureSession?.canAddOutput(stillImageOutput))!{
captureSession?.addOutput(stillImageOutput)
}

关于ios - 在 iOS10 中使用 AVCapturePhotoOutput - NSGenericException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57426551/

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