5

Looks like orientation for avcapturevideopreviewlayer has been depreciated in iOS 6. Anyone know the new code? Here is my current (depreciated) code:

[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
previewLayer.orientation = UIInterfaceOrientationLandscapeRight;
Marsman
  • 825
  • 1
  • 10
  • 20
  • Okey. Thats right. But was is your question? Probably that will help you: http://stackoverflow.com/questions/12404556/interface-orientation-in-ios-6-0 – NDY Oct 31 '12 at 14:40

3 Answers3

19

Did you check the documentation? It's only one line:

The layer’s orientation. (Deprecated in iOS 6.0. Use videoOrientation (AVCaptureConnection) instead.)

so use:

[[AVCaptureVideoPreviewLayer connection] setVideoOrientation: AVCaptureVideoOrientationLandscapeRight];

or

AVCaptureVideoPreviewLayer.connection.videoOrientation= AVCaptureVideoOrientationLandscapeRight;
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • Tried that. Property 'videoOrientation' not found in object of type 'AVCaptureVideoPreviewLayer' – Marsman Oct 31 '12 at 15:05
  • 'Cause it's in `AVCaptureConnection`? So, the `connection` property of `AVCaptureVideoPreviewLayer`. – J. Steen Oct 31 '12 at 15:15
  • I added an example for you:) I hope you don't mind @Nikolai Ruhe – Tieme Nov 01 '12 at 11:51
  • Shouldn't it be AVCaptureVideoOrientationLandscapeRight instead of UIInterfaceOrientationLandscapeRight? Both seems to work though. – Sten Nov 02 '12 at 16:12
  • 2
    This doesn't work - it does not have the same effect as changing .orientation. – Adam May 15 '13 at 13:14
  • Also: UIInterfaceOrientation* is the wrong type, and although it will sometimes work, you can get problems with e.g. orientations 180 degree flipped – Adam May 15 '13 at 13:15
  • Note this only works if the AVCaptureSession has been started. – Krumelur Dec 01 '14 at 18:49
3

I tried to use videoOrientation(AVCaptureConnection) instead of the deprecated orientation(AVCaptureVideoPreviewLayer), but it did not rotate the video preview anymore.

I replaced this:

AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;

With this:

AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;

But it did not rotate the video preview. The problem was that I added and modified the AVCaptureVideoPreviewLayer before I added the AVCaptureDeviceInput to my AVCaptureSession. Therefore the connection of my AVCaptureVideoPreviewLayer was null. The solution was to add the AVCaptureVideoPreviewLayer after I added the AVCaptureDeviceInput to my AVCaptureSession.

ediheld
  • 233
  • 3
  • 13
0

Just like @Nikolai posted, use the AVCaptureVideoPreviewLayer's connection's videoOrientation property instead.

(The reason I am posting this again is because his code may be a bit confusing since it looks like connection is a class method. Hopefully this example makes it clear.)

Replace the following:

AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.orientation = UIInterfaceOrientationLandscapeRight;

With:

AVCaptureVideoPreviewLayer *previewLayer = ...;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
Senseful
  • 86,719
  • 67
  • 308
  • 465