2

我正在尝试使用 iOS 7 的最新条形码 api 在检测到的条形码上绘制一个红色矩形。我已经设法拉入边界和角落,但我不太知道如何获取这些信息并将其应用到我的视图中。

在 viewDidLoad 方法中,我定义了一旦找到我将用来覆盖条形码的视图:

    //Initialize Laser View
    laserView = [[UIView alloc] init];
    laserView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
    laserView.layer.borderColor = [UIColor redColor].CGColor;
    laserView.layer.borderWidth = 2;
    [self.view addSubview:laserView];

在 didOutputMetadataObjects 中,我准备了激光视图并拉入条形码边界和角落:

//Prepare Laser View's frame
CGRect laser = CGRectZero; 

laser = barcodeObject.bounds;
NSLog(@"Bounds: %@", NSStringFromCGRect(barcodeObject.bounds));
NSLog(@"Frame: %@", barcodeObject.corners);

//Update Laser
laserView.frame = laser;

在我最近的一次跑步中,我得到了界限:{{7.0565414, 314.25}, {265.26062, 1.499999}} 和角落我得到了:

 (

    {
        X = "7.056541";
        Y = "314.25";
    },
        {
        X = "7.056541";
        Y = "315.75";
    },
        {
        X = "272.3172";
        Y = "315.75";
    },
        {
        X = "272.3172";
        Y = "314.25";
    }
)

我希望能够将 cgrect 紧紧地包裹在条形码周围。这是我正在尝试做的一个例子:

在此处输入图像描述

4

1 回答 1

1

您需要根据这些角构建一个CGPath或路径,然后绘制路径。UIBezierPath

  • 使用该CGPath方法,使用CGPathCreateMutable. 使用CGPathMoveToPoint设置起点创建路径,然后使用CGPathAddLineToPoint依次从那里移动到每个角落。完成后,您可以使用带有CAShapeLayer. 将该层添加为视图中的子层。
  • With UIBezierPath,遵循与 with 相同的一般方法CGPath。用于moveToPoint:开始和addLineToPoint:绘制形状。像往常一样绘制路径(通常在drawRect视图中)。
于 2014-02-11T21:58:03.920 回答