AVCaptureVideoPreviewLayer smooth orientation rotation

I'm trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. AVCaptureVideoPreviewLayer does have an orientation property, and changing it does allow for the layer to display properly for any orientation. However, the rotation involves some funky rotation of the AVCaptureVideoPreviewLayer, rather than staying smooth as it does in the Camera app.

This is how I've gotten orientation to work properly, minus the hitch in the rotation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    _captureVideoPreviewLayer.frame = self.view.bounds;
    _captureVideoPreviewLayer.orientation = [[UIDevice currentDevice] orientation];

How do I get this layer to act like the Camera app, while maintaining rotations for its subviews?

Also, as an aside, I've seen that the orientation property is depreciated on AVCaptureVideoPreviewLayer, and the videoOrientation property of AVCaptureConnection should be used instead, but I don't think I have an AVCaptureConnection to access here (I'm simply displaying the camera on the screen). How should I set this up to access videoOrientation?

Using an affine transform on the view containing the preview layer creates a smooth transition:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (cameraPreview) {
        if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
            cameraPreview.transform = CGAffineTransformMakeRotation(0);
        } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
            cameraPreview.transform = CGAffineTransformMakeRotation(M_PI/2);
        } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
            cameraPreview.transform = CGAffineTransformMakeRotation(-M_PI/2);
        cameraPreview.frame = self.view.bounds;

The willAnimateRotationToInterfaceOrientation function is called within the rotation animation block so changing properties here will animate along with the rest of the rotation animations. This function has been phased out in iOS 6 and replaced with new methods to handle device rotation, so this works for now, but isn't the best solution.

iphone, Using an affine transform on the view containing the preview layer creates a smooth transition: ios - avcapturevideoorientation - avcapturevideopreviewlayer prevent rotation AVCaptureVideoPreviewLayer orientation-need landscape (12)

This is an old question, but since it didn't have an accepted answer and I've been dealing with this issue myself for the past several days, I figured I would post the answer.

The trick to rotating the video based on device orientation changes, is to NOT ROTATE the AVCaptureVideoPreviewLayer or the AVCaptureConnection at all.

Changing the orientation on the AVCaptureConnection ( AVCaptureVideoPreviewLayer's orientation was deprecated ) ALWAYS results in an ugly animated change .. And after the video is rotated, you would still have to transform the preview layer.

The correct way to do this is use an AVAssetWriter to write the Video and Audio data .. and then apply a transform on the AVAssetWriterInput at the time that you start recording.

Here is a blurb from Apple about this --

Receiving rotated CVPixelBuffers from AVCaptureVideoDataOutput

To request buffer rotation, a client calls -setVideoOrientation: on the AVCaptureVideoDataOutput's video AVCaptureConnection. Note that physically rotating buffers does come with a performance cost, so only request rotation if it's necessary. If, for instance, you want rotated video written to a QuickTime movie file using AVAssetWriter, it is preferable to set the -transform property on the AVAssetWriterInput rather than physically rotate the buffers in AVCaptureVideoDataOutput.

The applying of the transform is similar to the code posted above.

  1. You register for device orientation changes.
  2. Keep track of transform to apply based on the new device orientation.
  3. When record is pressed, set up the AVAssetWriterInput and apply transform to it.

Hope this helps whoever is looking for an answer.

I'm stuck: How to get the same rotation behavior , I display filtered video, so using AVCaptureVideoPreviewLayer is not an option. the orientation property of its connection, as that will cause physical rotation of​  // Create a preview layer. let previewLayer = AVCaptureVideoPreviewLayer() // Connect the preview layer with the capturing session. previewLayer.session = captureSession // Add the preview layer into the view's layer hierarchy. view.layer.addSublayer(previewLayer)

I was able to accomplish this with the following code:

Set up when you need the camera:

preview = [[self videoPreviewWithFrame:CGRectMake(0, 0, 320, 480)] retain];
[self.view addSubview:preview];

The videoPreviewWithFrame function.

- (UIView *) videoPreviewWithFrame:(CGRect) frame {
  AVCaptureVideoPreviewLayer *tempPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]];
  [tempPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  tempPreviewLayer.frame = frame;

  UIView* tempView = [[UIView alloc] init];
  [tempView.layer addSublayer:tempPreviewLayer];
  tempView.frame = frame;

  [tempPreviewLayer autorelease];
  [tempView autorelease];
  return tempView;

orientation, orientation. The layer's orientation. Deprecated. Use videoOrientation ( AVCaptureConnection ) instead. SDK. iOS 4.0–6.0Deprecated. Framework. AVCaptureVideoPreviewLayer does have an orientation property, and changing it does allow for the layer to display properly for any orientation. However, the rotation involves some funky rotation of the AVCaptureVideoPreviewLayer, rather than staying smooth as it does in the Camera app.

I wanted the AVCaptureVideoPreviewLayer to handsomely follow all rotations. This is how I did it (overlayView is just a view that happens to have the correct bounds). It only animates without a hick-up when you place your calls to super exactly where they are in the code:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if ([[[self previewLayer] connection] isVideoOrientationSupported])
        [[[self previewLayer] connection] setVideoOrientation:toInterfaceOrientation];

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    CGRect layerRect = [[self overlayView] bounds];
    [[self previewLayer] setBounds:layerRect];
    [[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),

    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration: duration];

Prevent UIView with AVCaptureVideoPreviewLayer from rotating , I'm trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. Get it working How to fix screen auto-rotation problems on Windows 10 If your tablet is not longer auto-rotating when changing orientation, then use this guide to troubleshoot and fix this problem.

You should use CATransform3DMakeRotation which is meant for Layers, like it has been mentioned on the comments:

For instance:

float degreesToRotate = 90.0; //Degrees you want to rotate
self.previewLayer.transform = CATransform3DMakeRotation(degreesToRotate / 180.0 * M_PI, 0.0, 0.0, 1.0);

In my case, being "self.previewLayer" the layer to rotate. Have in mind that you should clip it to it's container view's bounds afterwards:

self.previewLayer.frame = self.view.bounds;

. . .

EDIT: If you are going to rotate as a result of the device being rotated (willAnimateRotationToInterfaceOrientation) you should make the transformation like this:

[CATransaction begin];
    [CATransaction setAnimationDuration:duration];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    //**Perform Transformation here**
    [CATransaction commit];

This way, the layer will rotate as the view does.

AVCaptureVideoPreviewLayer orientation - need landscape, Prevent UIView with AVCaptureVideoPreviewLayer from rotating with rest of ViewController - ios. videoOrientation = .portrait to extracting the UIView to a separate view controller, Animation on the BlurCropNode is not smooth enough. Smooth Interpolation of Orientations Gregory M. Nielson This is a slightly modified version of an earlier paper: Models and Techniques in Computer Animation, (Magnenat Thalmann N and Thalmann D, eds), Springer-Verlag, Tokyo, 1993, pp.75-93. ABSTRACT The problem of smoothly interpolating between a given sequence of orientations is discussed.

Disable shooting in any orientation other than Portrait AVFoundation , In my view hierarchy I have an AVCaptureVideoPreviewLayer as a sublayer in that previewLayer stays still and controls are smoothly reorganized. Right now since the main view is rotated on orientation change, my preview  A quaternion and its inverse refer to the same orientation, so x:+0.7 y:0 z:0 w:+0.7 == x:-0.7 y:0 z:0 w:-0.7 (but that is a different rotation from x:-0.7 y:0 z:0 w:+0.7). In the below example, I rotate around the X axis 3 times, and you can see the quaternions make a nice, smooth sinusoidal wave between +1 and -1.

I would like to prevent the camera itself to rotate so the viewer will see the camera AVCaptureVideoPreviewLayer* lay = [[AVCaptureVideoPreviewLayer alloc] instead of acos , smoother and takes account of all directions without fiddling. Start by navigating to the rotation property, then locate the stop watch icon to the right of the word "rotation." Simply hold ALT and click that stop watch icon. There should now be a space to the bottom right of your layer where you can start typing. Here is where we will be placing our expressions and coding in After Effects.

The LSL rotation type is one of several ways to represent an orientation in 3D. (Note that we try to write the type name in bold.). The rotation can be viewed as a discrete twist in three dimensional space, and the orientation of an object is how much it has been twisted around from whichever axes we are using - normally the region's axes.

  • Xcode is telling me that I cannot use the CGAffineTransformMakeRotation method and that for my previewlayer I need to use the CATransform3DMakeRotation method. Is your code wrong or am I doing something wrong?
  • @Will CGAffineTransformMakeRotation is for UIViews, CATransform3DMakeRotation is for CGLayers
  • willAnimateRotationToInterfaceOrientation:duration: is now deprecated. You can get the same result using viewWillTransitionToSize:withTransitionCoordinator: by assigning the transform/frame inside the animation block of [coordinator animateAlongsideTransition:completion:.
  • Having a similar problem using 'setVideoOrientation' so appreciate the comment Kal. Now that I know the issue, are there any examples out there which show this in action? BTW, the referenced link no longer works. Try this link: developer.apple.com/library/Mac/releasenotes/AudioVideo/…
  • Found an example and, not surprisingly, it was also from Kal. Here is the link: runkalrun.blogspot.com/2013/01/…. Thanks Kal, great info!
  • Drawback: for me, this solution is not enough. The snapshots I make from this camera preview, when loaded into an UIImageView, are in the original orientation of the camera preview. I will search for a separate solution for that, maybe pose a new question...
  • Next drawback: boss says this is way not handsome enough. So we do as they all do, fix the camera preview in portrait and rotate the controls listening to UIDeviceOrientation changing...
  • This's been deprecated in iOS 6. Use AVCaptureConnection's -setVideoOrientation:
  • willAnimateRotationToInterfaceOrientation was deprecated in iOS8
  • Can you please provide an alternative solution?
  • Take a look at Kal's answer. The best solution is to not rotate it.