iOS自定义键盘-相机无法使用

10

我想创建一个自定义键盘,可以作为条形码扫描器使用。我已经完成了整个编码过程,但输出结果不如预期: 第一次需要相机权限,但相机没有将视频发送到视图。

我认为,出于安全原因,可能存在某些限制使用键盘?!?

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];

唯一发生的事情就是键盘.barcodeView的背景颜色变成了红色。我这样做是为了确认我所做的所有接线都应该没问题。但没有来自摄像头的视频显示......

有人能帮帮我吗?


你在制作这个键盘方面有进展了吗?我也想制作一个相同的。 - Code Pope
1个回答

19
你之所以收到 null 的返回值,是因为你没有访问权限。这并不是一个 bug。根据苹果的指导方针,某些 API 不可用于 iOS 8 扩展(请参见下面的第三个要点)。

enter image description here

虽然有点不好,但我总是鼓励人们在开始一个想法之前,阅读新功能的相关信息并查看是否可行(这样可以节省很多时间)。一定要查看应用程序扩展编程指南以获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接