Ответ 1
У меня была такая же проблема перед тем, что решило мою проблему.
Я объявила глобальную переменную:
@interface YourViewController ()
{
...
UIDevice *iOSDevice;
}
..
@end
В implementation
(.m файл) я объявил наблюдателя NSNotification в -viewWillAppear
как:
@implementation YourViewController
-(void)viewWillAppear:(BOOL)animated
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
iOSDevice = notification.object;
previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation];
//or
[self setAutoVideoConnectionOrientation:YES];
}
Я сделал функцию, которая возвращает ориентацию скопированного текущего устройства, например:
Возьмите добычу по моему методу -interfaceOrientationToVideoOrientation
, он преобразует iOSDevice.orientation
в AVCaptureVideoOrientation
с тем, что у вас там есть.
(В моем случае мне нужны эти функции ниже, но посмотрите, как это может быть полезно для вас по какой-то причине)
- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation
{
switch (iOSDevice.orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
case UIDeviceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
}
}
- (AVCaptureConnection *)setAutoVideoConnectionOrientation:(BOOL)status
{
AVCaptureConnection *videoConnection = nil;
//This is for me
//for (AVCaptureConnection *connection in stillImageOutput.connections) {
//This might be for you
for (AVCaptureConnection *connection in previewLayer.connection) {
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo])
{
videoConnection = connection;
if (status == YES)
{
[videoConnection setVideoOrientation:[self interfaceOrientationToVideoOrientation]];
}
else
{
[videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}
}
if (videoConnection)
{
break;
}
}
}
return videoConnection;
}
Edit
Для ориентации по умолчанию: вам нужно проверить "текущую" ориентацию.
- (void)viewDidLoad
{
[super viewDidLoad];
// assuming you finish setting the `previewLayer`
..
// after all of that code, when the view is ready for displaying
// if you implemented this approach the best thing to do is:
// set this
iOSDevice = [UIDevice currentDevice];
// then call
previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation];
// to update the `previewLayer.connection.videoOrientation ` for default orientation
}
Примечание:
Используя NSNotificationCenter
, разрешите вращение устройства сначала перед запуском.
Надеюсь, это поможет вам.