Simple question, how do I change AVCaptureVideoPreviewLayer orientation when iPhone is rotated from portrait to landscape mode and vice versa.
I know this question has been asked many times and I am not the first one to run into it but I have tried every available solution that I could google or find but it still doesn't work.
The code I have implemented to setup AVCapture is. As you can see I am monitoring orientation changes
-(IBAction)InitiateCamera
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
//APPLE CODE
NSError *error = nil;
// Create the session
session = [[AVCaptureSession alloc] init];
// Configure the session to produce lower resolution video frames, if your
// processing algorithm can cope. We'll specify medium quality for the
// chosen device.
session.sessionPreset = AVCaptureSessionPresetLow;
// Find a suitable AVCaptureDevice
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Create a device input with the device and add it to the session.
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (!input) {
// Handling the error appropriately.
}
[session addInput:input];
// Create a VideoDataOutput and add it to the session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
// Configure your output.
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
// Specify the pixel format
output.videoSettings =
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey];
// If you wish to cap the frame rate to a known value, such as 15 fps, set
// minFrameDuration.
output.minFrameDuration = CMTimeMake(1, 15);
avLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
CGRect bounds=self.view.layer.bounds;
avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
avLayer.bounds=bounds;
avLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
//avLayer.backgroundColor = [[UIColor blackColor] CGColor];
[self.view.layer insertSublayer:avLayer atIndex:1];
// Start the session running to start the flow of data
[session startRunning];
// Assign session to an ivar.
[self setSession:session];
}
I used this post as an answer to try to change my AVCapture view orientation iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate
- (void) orientationChanged:(NSNotification *)notification
{
[self rotateLayer];
}
-(void)rotateLayer
{
CGRect layerRect = [[[self view] layer] bounds];
UIDeviceOrientation orientation =[[UIDevice currentDevice]orientation];
switch (orientation) {
case UIDeviceOrientationLandscapeLeft:
avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI+ M_PI_2); // 270 degress
break;
case UIDeviceOrientationLandscapeRight:
avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI_2); // 90 degrees
break;
case UIDeviceOrientationPortraitUpsideDown:
avLayer.affineTransform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
break;
default:
avLayer.affineTransform = CGAffineTransformMakeRotation(0.0);
[avLayer setBounds:layerRect];
break;
}
[avLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
}
The problem is that as you can see my screenshots, initially everything shows up correctly but after rotating the device the AVCapture view is all wacked out. Any suggestions as to what I am doing wrong in rotateLayer?