Search code examples
iphoneobjective-cavfoundationcalayercashapelayer

'Cut' a shape out of UIView or AVCaptureVideoPreviewLayer iPhone


I have a AVCaptureVideoPreviewLayer that needs to be cut so that it sits nicely onto my image that has parts that are transparent that need to be seen.

What is the best / efficient way to cut a shape out of the AVCaptureVideoPreviewLayer?

I have managed to get the correct shape using a CAShapeLayer but it seems AVCaptureVideoPreviewLayer doesn't have a Path property to do the same.

cameraLayer = [[CAShapeLayer layer] retain];
CGRect rect = CGRectMake(64, 32, 190 , 152);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, rect);
cameraLayer.path = path;
CGPathRelease(path);

*edit if it is possible to cut a shape out of a UIView or Imageview i should be able to do what i want aswell

Thanks


Solution

  • You said:

    I have managed to get the correct shape using a CAShapeLayer but it seems AVCaptureVideoPreviewLayer doesn't have a Path property to do the same.

    That's correct. Only CAShapeLayer has a path property, by design.

    If you want to cut a geometric hole in a view, the usual way to do that is to create a CAShapeLayer, install your shape path into it, and then make the shape layer the mask of the view's layer. This works well. Note that you need to set the opaque property on the view to NO in order to be able to see the content that's under the view through the "holes."