如何在AVCapture视频上添加CALayer rect形状?

时间:2013-05-31 15:01:01

标签: iphone ios

我想在AVCapture拍摄的视频上显示CALayer。 我能够显示图层,但是对于下一帧,应该删除前一个图层。

我的代码是:

[CATransaction begin];  
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

for (int i = 0; i < faces.size(); i++) {
     CGRect faceRect;
     // Get the Graphics Context        

     faceRect.origin.x = xyPoints.x;
     faceRect.origin.y = xyPoints.y;
     faceRect.size.width =50; //faces[i].width;
     faceRect.size.height =50;// faces[i].height;

     CALayer *featureLayer=nil;         

     // faceRect = CGRectApplyAffineTransform(faceRect, t);
     if (!featureLayer) {
         featureLayer = [[CALayer alloc]init];

         featureLayer.borderColor = [[UIColor redColor] CGColor];
         featureLayer.borderWidth = 10.0f;
         [self.view.layer addSublayer:featureLayer];

     }

     featureLayer.frame = faceRect;

     NSLog(@"frame-x - %f, frame-y - %f, frame-width - %f, frame-height - %f",featureLayer.frame.origin.x,featureLayer.frame.origin.y,featureLayer.frame.size.width,featureLayer.frame.size.height);
 }

//  [featureLayer removeFromSuperlayer]; 
[CATransaction commit];

其中face是(const std::vector<cv::Rect)face OpenCV格式。 我需要知道代码[featureLayer removeFromSuperLayer];

的放置位置

注意:“脸部”不适用于脸部检测......它只是一个矩形。

1 个答案:

答案 0 :(得分:1)

我有解决方案...... featureLayer是CALayer对象,我将其作为标识。喜欢

featureLayer.name = @"earLayer";

每当我在帧中检测到Object时,我都会从主视图中获取子层,如

NSArray *sublayers = [NSArray arrayWithArray:[self.view.layer sublayers]];

并计算子图层以检查循环,如下所示:

int sublayersCount = [sublayers count];
    int currentSublayer = 0;
for (CALayer *layer in sublayers) {
        NSString *layerName = [layer name];
        if ([layerName isEqualToString:@"earayer"])
            [layer setHidden:YES];
}

现在我正在使用Detected objects获取正确的图层。