12

私はAVFoundationカメラを見せるために使っています。

カメラ自体が回転しないようにしたいので、視聴者はカメラをポートレートでのみ表示し、画像はポートレートモードでのみ撮影されます。

ポートレートのみをサポートするように定義Supported Interface Orientationしましたが、ビュー自体はポートレートモードでのみ表示され、カメラは表示されません-デバイスの向きで回転しています

AVFoundationUIViewControllerのように、カメラを強制的に表示して、ポートレートでのみ画像をキャプチャするにはどうすればよいですか?

カメラを設定するための私のコード:

AVCaptureVideoPreviewLayer* lay = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.sess];
UIView *view = [self videoPreviewView];
CALayer *viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
[lay setFrame:bounds];
if ([lay respondsToSelector:@selector(connection)])
 {
   if ([lay.connection isVideoOrientationSupported])
   {
      [lay.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    }
 }
 [lay setVideoGravity:AVLayerVideoGravityResizeAspectFill];
 [viewLayer insertSublayer:lay below:[[viewLayer sublayers] objectAtIndex:0]];
 self.previewLayer = lay;
4

3 に答える 3

2

カメラの回転を制限するには、このメソッドを使用する必要があると思います。

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIDevice currentDevice].orientation];
}

プレビューレイヤーがプロパティとして定義されていると仮定すると、使用できます

[self.previewLayer setOrientation:[[UIDevice currentDevice] orientation]];

あなたの場合、 [[UIDevice currentDevice] orientation] を UIInterfaceOrientationPortrait に置き換えることができます

編集済み

実際に必要なときにプレビュー レイヤーを追加してみてください。例

preview = [[self videoPreviewWithFrame:CGRectMake(0, 0, 320, 480)] retain];
[self.view addSubview:preview];

videoPreviewWithFrame 関数。

- (UIView *) videoPreviewWithFrame:(CGRect) frame 
{
  AVCaptureVideoPreviewLayer *tempPreviewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:[self captureSession]];
  [tempPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  tempPreviewLayer.frame = frame;

  UIView* tempView = [[UIView alloc] init];
  [tempView.layer addSublayer:tempPreviewLayer];
  tempView.frame = frame;

  [tempPreviewLayer autorelease];
  [tempView autorelease];
  return tempView;
}
于 2013-02-10T05:29:35.847 に答える
2

プレビューレイヤーがviewcontrollerビューに追加されたと仮定します。でこれを行いviewDidLoadます:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

セレクターを次のように定義します。

- (void)orientationChanged:(NSNotification*)notification {
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    if ([self.previewlayer respondsToSelector:@selector(orientation)]) {
        //for iOS5
        if (interfaceOrientation != UIInterfaceOrientationPortrait) {
            self.previewlayer.orientation = (AVCaptureVideoOrientation)UIInterfaceOrientationPortrait;
        }
    } else {
        //for iOS6
        if (interfaceOrientation != UIInterfaceOrientationPortrait) {
            self.previewlayer.connection.videoOrientation = (AVCaptureVideoOrientation)UIInterfaceOrientationPortrait;
        }
    }
}

注:tempPreviewLayerプロパティに入れますself.previewlayer

これにより、デバイスの向きが変更されたときに、プレビュー レイヤーが強制的に縦位置になります。

編集

これをviewControllerの「shouldAutoRotate」メソッドに追加することもできます

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        if ([self.previewlayer respondsToSelector:@selector(orientation)]) {
            if (interfaceOrientation != UIInterfaceOrientationPortrait) {
                self.previewlayer.orientation = (AVCaptureVideoOrientation)UIInterfaceOrientationPortrait;
            }
        } else {
            if (interfaceOrientation != UIInterfaceOrientationPortrait) {
                self.previewlayer.connection.videoOrientation = (AVCaptureVideoOrientation)UIInterfaceOrientationPortrait;
            }
        }

        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

ios6 の場合は、これら 2 つを上書きして確認します。

-(NSUInteger)supportedInterfaceOrientations {
    //UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    //return (
    //interfaceOrientation == UIInterfaceOrientationLandscapeLeft |
    //interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    return UIInterfaceOrientationMaskLandscape;//(UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}

- (BOOL)shouldAutorotate {
    return YES;
}

returnこれらの2つのメソッドでコードを追加する前に..そして私が与えた通知で、デバイスを回転させたときに呼び出されるかどうかを確認してください。

于 2013-02-11T10:10:00.757 に答える