2014-09-21 87 views
9

我想创建一个自定义键盘,充当条形码扫描仪。 我已经完成了整个编码,但输出结果并不如预期:我被要求提供相机权限(第一次),但相机不向视图发送视频。iOS自定义键盘 - 相机不能正常工作

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

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改变其背景颜色为红色。我已经明白了,我所做的所有布线应该都是好的。但没有显示摄像机的视频....

任何人都可以帮我吗?

+0

您在创建此键盘方面有任何进展吗?我想创建相同的 – 2015-09-24 14:14:37

回答

18

你收回空的原因是你没有权限访问它。这实际上不是一个错误。根据Apple准则,某些API不适用于iOS 8扩展(请参阅下面的第3项)。

enter image description here

它很烂,但我总是鼓励人们对新功能的阅读,看看是否他们想要做什么是可能的,到居住的想法之前(节省了大量的时间)。肯定检查出App Extension Programming Guide欲知更多信息。