gpt4 book ai didi

ios - AVCaptureOutput captureStillImageAsynchronouslyFromConnection 在 iPhone5 上永远不会完成

转载 作者:可可西里 更新时间:2023-11-01 03:21:32 25 4
gpt4 key购买 nike

在我的应用程序中,我显示了一个 AVCaptureVideoPreviewLayer,然后当用户使用 AVCaptureOutput 中的 captureStillImageAsynchronouslyFromConnection 函数单击按钮时捕获静止图像.在 iPhone 5 之前,这对我来说一直很有效,但它从未完成。

我的设置代码是:

...
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.imageOutput setOutputSettings:outputSettings];

self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
[self.captureSession addInput:self.rearFacingDeviceInput];
[self.captureSession addOutput:self.imageOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.frame = CGRectMake(0, 0, 320, 427);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

[self.captureSession startRunning];
[outputSettings release];

我的捕获方法是:

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.imageOutput.connections){
for (AVCaptureInputPort *port in [connection inputPorts]){
if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}

//code to abort if not return 'soon'
...

[self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error){

//use image here

}];

captureStillImageAsynchronouslyFromConnection 永远不会为我使用 iPhone5 完成

我测试过:

  • 它不是 OS 6,因为此代码适用于已更新的 iPhone 4s 和 iPod(iPod touch(第 4 代))

  • captureSession 正在运行

  • videoConnection 不为零

  • imageOutput 不为零

还有:

  • 我正在使用此方法而不是 UIImagePickerController,因为我需要将预览作为 subview 。

  • 在捕获 Session 时调用 stopRunning 在 iPhone 5 上也需要几秒钟

最佳答案

好吧,这段代码工作正常。针对 iPhone 4 和 5 进行了测试(baseSDK 7.1,在 ARC 下)。

您必须考虑的几件事。

1) 确保正确设置 rearFacingDeviceInput,

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[self setRearFacingDeviceInput:[AVCaptureDeviceInput deviceInputWithDevice:device error:nil]];

2) 作为 Vincent提到,会报错,尝试同时记录错误和imageSampleBuffer

3) session 的 -startRunning 和 -stopRunning 操作需要很长时间才能完成(几秒,甚至 5-6 秒),这些方法在完成所有工作之前不会返回,为避免阻塞 UI,您不应调用这些方法主线程上的方法,一种方法是使用 GCD

dispatch_queue_t serialQueue = dispatch_queue_create("queue", NULL);
dispatch_async(serialQueue, ^{
[self.captureSession startRunning];
});

如果 captureStillImageAsynchronously 仍然没有完成(为确保这一点,在 block 中添加断点,并记录所有内容),您应该检查设备的相机。我相信您的代码适用于所有 iPhone 5 设备。希望这对您有所帮助,祝您好运。

关于ios - AVCaptureOutput captureStillImageAsynchronouslyFromConnection 在 iPhone5 上永远不会完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590330/

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