How can I get 'AVCaptureVideoPreviewLayer' to display properly in landscape orientation? It works fine in portrait but doesn't rotate, and shows a rotated camera capture when the parent view controller is in landscape orientation.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
Besides viewWillLayoutSubviews(), you also have to implement didRotateFromInterfaceOrientation().
This is because the subview layouts won't be updated if the device rotates from portrait mode to portrait upside-down mode (obviously for efficiency reasons).
First, the answer
I've come across several SO posts on this matter, and couldn't find any simple explanation so I thought I'd share my own.
If you follow Apple's sample on the matter, you'll run into two potential problems when you rotate your iOS device to landscape
The problem here is that 'CALayer' doesn't support autorotation hence, unlike a 'UIView' you'd add as a subview, it won't rotate when its parent 'UIView' rotates. Therefore, you have to manually update its frame every time the parent view's bounds changes (not parent view's frame since frame stays the same after rotation). This is achieved by overriding 'viewWillLayoutSubviews' in the container view controller.
Secondly, you should use 'videoOrientation' property to inform AVFoundation about the orientation so it does the preview properly.
Hope this helps.
Swift3
//SWIFT 3 CONVERSION