Search code examples
iostransformcalayerscale

CALayer content goes out of bounds - iOS


I am trying to implement camera zoom using CGAffinetransform. Transform is fine, but when I scale it to a bigger size, it goes out of the frame I have assigned to the AVCaptureVideoPreviewLayer. I tried setting masksToBounds property to YES but it didn't help.

Can I contain it within its frame?

Edit:
What I want is that I can specify a specific area for the camera preview layer, if I apply scaling transform to it, (i.e., frame of preview layer gets expanded), the part of the layer outside of the specified area gets clipped.


Solution

  • You should put the layer you are scaling inside of another layer and mask that one instead (the superlayer). The same thing works with views.

    I.e. You have two views / layers: clippingView and scalingView where scalingView is the subview of clippingView and clippingView is the view that actually clips to it's bounds.

    [clippingView addSubview:scalingView];
    clippingView.clipsToBounds = YES;
    

    or using layers

    [clippingLayer addSublayer:scalingLayer];
    clippingLayer.masksToBounds = YES;