Пейзаж в AvCam iOS 6
Я новичок в iOS, пытаюсь создать собственную камеру с помощью AvCam. У меня возникли проблемы с предварительным просмотром альбомной ориентации - он поворачивает изображение на 90 градусов по часовой стрелке и показывает его на половине экрана.
Я получаю это сообщение -
ВНИМАНИЕ: -[ setOrientation:] устарело.
Пожалуйста, используйте AVCaptureConnection -setVideoOrientation:
AVCaptureConnection уже устанавливает ориентацию, поэтому я понятия не имею, что я должен еще.
Я знаю, что этот вопрос задавался много раз для предыдущих версий iOS (4,5), но ни один из этих методов / кодов не работал для меня (iOS 6).
Оригинальный код (без изменений от Apple)
if ([self captureManager] == nil) {
AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
[self setCaptureManager:manager];
[manager release];
[[self captureManager] setDelegate:self];
if ([[self captureManager] setupSession]) {
// Create video preview layer and add it to the UI
AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
UIView *view = [self videoPreviewView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[newCaptureVideoPreviewLayer setFrame:bounds];
if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
[newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait];
}
[newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
[self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
AVCaptureConnection чанк:
-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation; {
AVCaptureConnection *videoConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self movieFileOutput] connections]];
if ([videoConnection isVideoOrientationSupported])
[videoConnection setVideoOrientation:videoOrientation];
[[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL] recordingDelegate:self];
}
2 ответа
В прошлый раз я тоже наткнулся на эту проблему. Я решил эту проблему, сделав две вещи
Получение правильной ориентации
замещатьif ([newCaptureVideoPreviewLayer isOrientationSupported]) { [newCaptureVideoPreviewLayer setOrientation:AVCaptureVideoOrientationPortrait]; }
С
if ([newCaptureVideoPreviewLayer.connection isVideoOrientationSupported]) { [newCaptureVideoPreviewLayer.connection setVideoOrientation:[UIDevice currentDevice].orientation]; }
Принудительно обновлять ориентацию видео во время инициализации, чтобы перехватывать видео в альбомном режиме, вызывая
- (void)deviceOrientationDidChange
вручную в течениеAVCaptureManager.m
Я добавил это к:
- (BOOL) setupSession { BOOL success = NO; ... AVCamRecorder *newRecorder = [[AVCamRecorder alloc] initWithSession:[self session] outputFileURL:self.lastOutputfileURL]; [newRecorder setDelegate:self]; [self performSelector:@selector(deviceOrientationDidChange)]; ... return success; }
Итак, вот обходной путь, но я уверен, что должно быть лучшее решение, чем это. Я получил часть кода из этого вопроса:
Приложение для iPhone - Показать видео AVFoundation в ландшафтном режиме
Но пришлось настроить фрейм для каждой ориентации, чтобы он работал на iOS6 (и он все еще показывает предупреждение):
- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration {
[CATransaction begin];
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeLeft;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
} else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationPortrait;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 320, 480);
} else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
captureVideoPreviewLayer.orientation = UIInterfaceOrientationLandscapeRight;
captureVideoPreviewLayer.frame = CGRectMake(0, 0, 480, 320);
}
[CATransaction commit];
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}