Swatee Salunkhe
Swatee Salunkhe

Reputation: 121

How to add CALayer rect shape on AVCapture video?

I want to display a CALayer on video captured by AVCapture. I am able to display the layer but for the next frame the previous should be removed.

My code is:

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

where face is (const std::vector<cv::Rect)face OpenCV format. I need to know where to place the code [featureLayer removeFromSuperLayer];

Note: "face" is not for face detection... it is just a rectangle.

Upvotes: 2

Views: 236

Answers (1)

Swatee Salunkhe
Swatee Salunkhe

Reputation: 121

I have got the solution ... featureLayer is CALayer object I gave this as identity. Like

featureLayer.name = @"earLayer";

and whenever I detect the Object in frame I get the sublayers from main view like

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

and count the sublayer to check in for loop like below:

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

Now I am getting the proper layer with Detected objects.

Upvotes: 1

Related Questions