iOS自定义键盘 – 相机不工作

我想创build一个自定义的键盘,作为条形码扫描仪。 我已经完成了整个编码,但是输出结果并不如预期的那样:我被要求提供相机权限(第一次),但是相机并没有发送video给视图。

我想,出于安全的原因,使用键盘可能会有一些限制吗?

1.)打开火炬

-(void) turnFlashOn { AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn]) { BOOL success = [flashLight lockForConfiguration:nil]; if(success){ NSError *error; [flashLight setTorchMode:AVCaptureTorchModeOn]; [flashLight setTorchModeOnWithLevel:1.0 error:&error]; NSLog(@"Error: %@", error); [flashLight unlockForConfiguration]; NSLog(@"flash turned on -> OK"); } else { NSLog(@"flash turn on -> ERROR"); } } } 

这给了我这个日志输出,但闪光灯没有任何反应:

 Error: (null) flash turned on -> OK 

2.)扫描条码(viewDidLoad的一部分)

  // SCANNER PART self.captureSession = [[AVCaptureSession alloc] init]; AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error]; if(videoInput) [self.captureSession addInput:videoInput]; else NSLog(@"Error: %@", error); AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init]; [self.captureSession addOutput:metadataOutput]; [metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]]; AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; camView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; previewLayer.frame = camView.layer.bounds; [camView.layer addSublayer:previewLayer]; self.keyboard.barcodeView.clipsToBounds=YES; camView.center = CGPointMake(self.keyboard.barcodeView.frame.size.width/2, self.keyboard.barcodeView.frame.size.height/2); [self.keyboard.barcodeView addSubview:camView]; 

如果我按下我的键盘上的一个特殊的键,这个叫做:

 -(void)scanBarcodeNow{ AudioServicesPlaySystemSound(systemSoundTock); NSLog(@"Start scanning..."); self.keyboard.barcodeView.hidden=false; [self.keyboard.barcodeView addSubview:camView]; [self.keyboard.barcodeView setBackgroundColor:[UIColor redColor]]; [self.captureSession startRunning]; 

}

唯一发生的是,keyboard.barcodeView将其背景颜色更改为红色。 我已经明白了,我所做的所有布线都应该是正确的。 但是没有显示来自摄像头的video….

谁能帮我吗?

你回到空的原因是因为你没有访问权限。 这实际上不是一个错误。 根据苹果的指导方针,某些API不适用于iOS 8扩展(请参阅下面的第3项)。

在这里输入图像说明

这很糟糕,但是我总是鼓励大家阅读新的function,看看他们想要做什么是可能的,然后再想一想(节省很多时间)。 绝对检查应用程序扩展编程指南了解更多信息。