CALayer content goes out of bounds - iOS
Solution 1
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;
Solution 2
You guys are all partially right I found but I wanted to clarify.
Lets say we added something like AVCaptureVideoPreviewLayer to the view via [self.view.layer addSublayer:previewLayer]
[self clipsToBounds] does NOTHING until you are telling its primary layer to mask to bounds.
[self.view.layer masksToBounds];
Just because your view has a frame and so does its layers DOES NOT MEAN IT HAS BOUNDS. If it doesnt have bounds then there is nothing to mask to. So do this
self.view.layer.bounds = self.view.frame
;
So heres it all together..keep in mind I did this in my own UIView class so I dont need to call self.view.
previewLayer.bounds = self.frame;
self.layer.bounds = self.frame;
self.layer.masksToBounds = YES;
previewLayer.masksToBounds = YES;
[self setBounds:self.frame];
[self clipsToBounds];
neeraj
Updated on July 21, 2022Comments
-
neeraj almost 2 years
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 theAVCaptureVideoPreviewLayer
. I tried settingmasksToBounds
property toYES
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. -
neeraj over 11 yearsI tried adding a separate view, (
UIImageView
because of some reason, but that shouldn't matter I suppose) and added a sublayer as the previewLayer which I was setting as the subLayer ofself.view.layer
. I setmasksToBounds' as
YES` but still it goes out of bounds -
neeraj over 11 yearsWhat I did was, I made a view, with the frame that I wanted the preview layer to be in. and i set its property
clipsToBounds
to YES. That was all. Doc says that all the content of the view will lie inside the specified frame if this property is set to Yes. Actually I never came across the property before now. -
David Rönnqvist about 11 yearsAny reason in particular for changing the accepted answer? You are basically saying the same thing as I am.
-
neeraj about 11 years1) Your answer was not the accepted answer before this, probably because I didn't understand it at that time. Confused between layers and views. 2) Yesterday I looked at my answer, and saw that there was no accepted answer, so I accepted my own...
-
neeraj about 11 yearsBut now I see that yours was accepted. So, a mistake has been done.
-
Nathan Kellert over 9 yearsSo is that all you had to change. Im having the exact same problem. the layer gets larger than the frame itself. I setup a NSLog to verify that the views fram itself wasnt getting larger. even though I have clipsToBounds on the view set to yes, and maskstobounds set to yes on the preview layer...the preview layer is still going outside.
-
Kunal Shrivastava almost 9 yearsThis is exactly what I was looking for. Even I have a custom UIView and I want to show a camera using AVFoundation. Thanks!