在相机视图中显示 UIView



我正在制作一个在视图中具有相机预览的应用程序,在此视图中,我想绘制相机数据以及另一个视图,该视图在捕获数据时显示一个小矩形,例如,在条形码扫描场景中,相机显示在视图中,当找到条形码时,将绘制一个矩形,显示它已扫描条形码。我目前的视图层次结构如下:

View
{
  -UIView cameraHolder
   {
        -UIView highlightView
   }
}

我设法显示相机并扫描东西,但突出显示视图没有显示,为什么会发生这种情况?

这是初始化突出显示视图的代码:

-(void)setUpHiglightView{
    self.highlightView = [[UIView alloc] init];
    self.highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    self.highlightView.layer.borderColor = [UIColor greenColor].CGColor;
    self.highlightView.layer.borderWidth = 3;
}

这是捕获数据时的代码:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
    for(AVMetadataObject *metadata in metadataObjects){
        for(NSString *type in barCodeTypes){
            if([metadata.type isEqualToString:type]){
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject*)metadata];
                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject*)metadata stringValue];
                break;
            }
        }
    }

    if(detectionString != nil){
        [self.itemIdTextField setText:detectionString];
    }else{
        //NSLog(@"Got Nothing");
    }
    NSLog(@"Position: [%f,%f][%f,%f]",highlightViewRect.origin.x, highlightViewRect.origin.y,highlightViewRect.size.height, highlightViewRect.size.width);
    self.highlightView.frame = highlightViewRect;
}

还有初始化相机的代码:

-(void)setupBarCodeScanner{
    [self setUpHiglightView];
    session = [[AVCaptureSession alloc] init];
    device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;
    input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if(input){
        [session addInput:input];
    }else{
        [self showAlertDialogWithTitle:@"Error" andMessage:@"There was an error while accessing your camera"];
        NSLog(@"Error: %@", error);
    }
    output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutput:output];
    output.metadataObjectTypes = [output availableMetadataObjectTypes];
    prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    prevLayer.frame = self.cameraHolder.bounds;
    prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.cameraHolder.layer addSublayer:prevLayer];
}

谢谢!

在 captureOutput 末尾将 self.highlightView 添加到 self.view:

[self.view addSubview:self.highlightView];

看起来您没有在任何地方添加该视图。您创建它并设置其frame,但我没有看到您添加到视图层次结构的位置。

最新更新