我在叠加视图中打开相机以扫描QR码。它通常工作正常,但每15-20次左右,相机会在第一帧后立即冻结。该应用程序仍在运行,如果您按下我们的后退按钮并再次返回相机,它通常不会再次冻结。它发生在带有iOS 7.1的iPhone 4和带有iOS 8.3的iPhone 5上,这是我唯一可以访问它的手机。据我所知,冻结是完全随机的。如果我将captureOutput
委托功能冻结,如果我将其瞄准QR码,则相机不会运行NSTimer
委托功能,因此看起来它不仅仅是冻结的预览。
我甚至无法弄清楚如何以编程方式告诉它是否冻结。使用函数记录self.captureSession.isRunning
运行- (void)viewDidLoad {
[ super viewDidLoad ];
// Camera / QR-Scanner
NSError *error;
self.captureSession = nil;
self.cameraView = [ [ UIView alloc ] initWithFrame:self.view.bounds ];
self.cameraView.backgroundColor = [ UIColor colorWithRed:255.0f green:0.0f blue:0.0f alpha:0.5f ];
AVCaptureDevice *captureDevice = [ AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo ];
AVCaptureDeviceInput *input = [ AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error ];
if ( input ) {
if ( [ captureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus ] && [ captureDevice isFocusPointOfInterestSupported ] ) {
if ( [ captureDevice lockForConfiguration:&error ] ) {
[ captureDevice setFocusPointOfInterest:CGPointMake( 0.5f, 0.5f ) ];
[ captureDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus ];
[ captureDevice unlockForConfiguration ];
}
}
self.captureSession = [ [ AVCaptureSession alloc ] init ];
[ self.captureSession addInput:input ];
[ self.captureSession setSessionPreset:AVCaptureSessionPreset352x288 ];
AVCaptureMetadataOutput *captureMetadataOutput = [ [ AVCaptureMetadataOutput alloc ] init ];
[ self.captureSession addOutput:captureMetadataOutput ];
dispatch_queue_t dispatchQueue = dispatch_queue_create( "cqQRScanner", NULL );
[ captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue ];
[ captureMetadataOutput setMetadataObjectTypes:[ NSArray arrayWithObject:AVMetadataObjectTypeQRCode ] ];
self.videoPreviewLayer = [ [ AVCaptureVideoPreviewLayer alloc ] initWithSession:self.captureSession ];
[ self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill ];
[ self.captureSession startRunning ];
/* Also tried this:
dispatch_async( dispatch_get_main_queue(), ^{
if ( ![ self.captureSession isRunning ] ) {
[ self.captureSession startRunning ];
}
} );
*/
} else {
CLS_LOG( @"%@", [ error localizedDescription ] );
}
}
即使在冻结后也始终输出true。
这是我正在使用的代码。我删除了不相关的代码,但在相机预览视图的顶部有一些视图。如果您认为它们具有相关性,我可以将它们重新添加。
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector( didStop ) name:@"AVCaptureSessionDidStopRunningNotification" object:nil ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector( didStart ) name:@"AVCaptureSessionDidStartRunningNotification" object:nil ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector( didRuntimeError ) name:@"AVCaptureSessionRuntimeErrorNotification" object:nil ];
我还添加了以下内容:
- ( void ) didStop {
CLS_LOG( @"Camera did stop" );
}
- ( void ) didStart {
CLS_LOG( @"Camera did start" );
}
- ( void ) didRuntimeError {
CLS_LOG( @"Camera did runtime error!" );
}
再加上这些(CLS_LOG是通过Crashlytics的NSLog):
mystring == null
但它冻结时也不会发送运行时错误。
你有什么想法吗?非常感谢任何帮助!
修改 完成帖子后,我发现相机已经解冻了。它需要10-15分钟才能完成,我现在正在iPhone 5s上重新测试它。是不是它实际上没有停止,只是停顿了很长时间?应用程序的其余部分仍然正常运行。
更新2:似乎进入主屏幕并重新进入应用程序有时会再次解锁相机。