gpt4 book ai didi

swift - AVCapturePhoto 为什么我的照片总是为零?

转载 作者:行者123 更新时间:2023-11-30 12:04:10 26 4
gpt4 key购买 nike

我正在尝试实现一些类似于 snapchat 的自定义相机。我不明白为什么我的图像在继续过程中总是为零。也许一双新的眼睛可能会有所帮助,因为我已经为此工作了两天......

当我点击拍照按钮(运行“livePhotoTapped”)时,应用程序崩溃并出现错误: fatal error :在展开可选值时意外发现 nil,指的是图像为零

任何帮助都会很好:)

@IBOutlet weak var cameraView: UIView!

//session to capture data
var captureSession = AVCaptureSession()
//which camera to use
var backFacingCamera: AVCaptureDevice?
var frontFacingCamera: AVCaptureDevice?
var currentDevice: AVCaptureDevice?

var stillImageOutput: AVCaptureStillImageOutput?
var stillImage: UIImage?

//camera preview layer
var cameraPreviewLayer: AVCaptureVideoPreviewLayer?


func setupCaptureSessionCamera() {
//this makes sure to get full res of camera
captureSession.sessionPreset = AVCaptureSession.Preset.photo

var devices = AVCaptureDevice.devices(for: .video)

//query available devices
for device in devices {

if device.position == .front {
frontFacingCamera = device
} else if device.position == .back {
backFacingCamera = device
}
}//end iteration

//set a default device
currentDevice = backFacingCamera

//configure session w output for capturing still img
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecType.jpeg]


do {
//capture the data from whichevr camera we r using..imagine as buffer to translate data into real world image
let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)

captureSession.addInput(captureDeviceInput)
captureSession.addOutput(stillImageOutput!)

//setup camera preview layer
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

//add the preview to our specified view in the UI
view.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.frame = cameraView.frame

captureSession.startRunning()

} catch let error {

print(error)

}//end do
}

@IBAction func livePhotoTapped(_ sender: UIButton) {

let videoConnection = stillImageOutput?.connection(with: .video)

//capture still image async
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection!, completionHandler: { (imageDataBuffer, error) in

if let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: imageDataBuffer!, previewPhotoSampleBuffer: imageDataBuffer!) {

self.stillImage = UIImage(data: imageData)
self.performSegue(withIdentifier: "toPreviewPhoto", sender: self)
}

})
}

最佳答案

像这样使用:

对于 CMSampleBufferIsValid 检查此 link

@IBAction func livePhotoTapped(_ sender: UIButton) {

let videoConnection = stillImageOutput?.connection(with: .video)

//capture still image async
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection!, completionHandler: { (imageDataBuffer, error) in

if error != nil {
print("error \(error)")
} else {
if let imageBuffer = imageDataBuffer, CMSampleBufferIsValid(imageBuffer) {
if let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: imageDataBuffer!, previewPhotoSampleBuffer: imageDataBuffer!) {

self.stillImage = UIImage(data: imageData)
self.performSegue(withIdentifier: "toPreviewPhoto", sender: self)
}
} else {
print("imageDataBuffer is nil or not valid")
}
}
})
}

关于swift - AVCapturePhoto 为什么我的照片总是为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858831/

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