2014-09-21 85 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欲知更多信息。