Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 950
neeraj Avatar asked Jan 09 '13 12:01

neeraj


2 Answers

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;
like image 134
David Rönnqvist Avatar answered Oct 30 '22 14:10

David Rönnqvist


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]

  1. [self clipsToBounds] does NOTHING until you are telling its primary layer to mask to bounds. [self.view.layer masksToBounds];

  2. 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];
like image 39
Nathan Kellert Avatar answered Oct 30 '22 12:10

Nathan Kellert