I\'m trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. AVCaptureVideoPreviewLayer does h
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.
Hope this helps whoever is looking for an answer.
After examining Apple Example, I have figured out how to disable the rotation of your videoPreviewLayer
in iOS 11 and later. (not sure if the same works for older version)
Simply set the clipToBounds = false
on the UIView
of your videoPreviewLayer
.
Hope this helps.
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),
CGRectGetMidY(layerRect))];
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration: duration];
}