Ok, I solved it.
The problem is that when using autorun the frames / borders of your subframes, the UIViewController
may change after you set up your previewLayer
, without updating the previewLayer
! This is because the CALayer
layout CALayer
controlled by only one native code (see also this related answer: fooobar.com/questions/979878 / ... ).
To solve this problem, you must override the viewDidLayoutSubviews
method of your UIViewController
to update the position and previewLayer
frames (and possibly others) every time you change the layout of the subview.
In the future, I will describe how I solved this in detail:
My case was actually a bit more complicated because I am using OpenCV iOS framework 2.4.9. Here, a preview layer is created and managed by an OpenCV class called CvVideoCamera
. So I had to create a subclass of CvVideoCamera
, to which I added the following method:
- (void)updatePreviewLayer { self.customPreviewLayer.bounds = CGRectMake(0, 0, self.parentView.frame.size.width, self.parentView.frame.size.height); [self layoutPreviewLayer]; }
Explanation: parentView
is a UIView that you pass to CvVideoCamera
to indicate where the preview layer should be located and how much it should be. customPreviewLayer
is a preview layer managed by CvVideoCamera. And layoutPreviewLayer
is a method that basically updates the position of the layer: https://github.com/Itseez/opencv/blob/2.4/modules/highgui/src/cap_ios_video_camera.mm#L211
Then I just called this method from my UIViewController as follows:
- (void)viewDidLayoutSubviews { NSLog(@"viewDidLayoutSubviews"); [super viewDidLayoutSubviews]; [self.myCameraView updatePreviewLayer]; }
source share