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将其背景颜色更改为红色.我已经看到,我所做的所有接线应该是好的.但是没有显示凸轮的视频….

谁能帮我吗?

解决方法

您要恢复为空的原因是因为您无法访问它.这其实不是一个bug.根据苹果指南,某些API不适用于iOS 8扩展(请参阅下面的第3项).

它吸吮,但我总是鼓励人们阅读新的功能,看看他们想做什么是可能的,在住在一个想法(节省很多时间)之前.肯定查看App Extension Programming Guide了解更多信息.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...