5

を初期化し、次のAVCaptureSessionようにプリセットします。

AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
if (YES==[newCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
    newCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;
} else {
    // Error management
}

次に、次をセットアップしますAVCaptureVideoPreviewLayer

self.preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/*426*/)];
CALayer *previewLayer = preview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
captureVideoPreviewLayer.frame = previewLayer.frame;
[previewLayer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

私の質問は次のとおりです。画面にすべてのレイヤーを表示するために必要
な正確なものを取得するにはどうすればよいですか? より正確には、フィットするように高さが必要ですか?ぴったり合う AVCaptureVideoPreviewLayer サイズを取得しようとしています。CGSizecaptureVideoPreviewLayerAVLayerVideoGravityResizeAspectAVCaptureVideoPreviewLayerpreview.size

ご協力ありがとうございました

4

4 に答える 4

8

AVCaptureSessionPresetPhotoで調査した後、AVCaptureVideoPreviewLayerはiPhoneカメラの3/4の比率を尊重します。したがって、単純な微積分で適切な高さを簡単に得ることができます。
たとえば、幅が320の場合、適切な高さは次のようになります
。320* 4/3 = 426.6

于 2013-01-06T20:27:57.343 に答える
1

gsempe さん、ご回答ありがとうございます。私は何時間も同じ問題を抱えています:)そして、横向きモードで画面の中央に配置するために、このコードで解決しました:

CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:CGRectMake(0, 0, 426.6, 320)];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidY(layerRect), CGRectGetMidX(layerRect))];

CGRectGetMidY() 関数と CGRectGetMidX() 関数を反転して、レイヤーを画面の中央に配置する必要があることに注意してください。

ありがとう、

ジュリアン

于 2014-03-23T22:03:24.840 に答える
1

私が正しく理解している場合、現在のビデオ セッションの幅と高さを取得しようとしています。
の outputSettings ディクショナリから取得できますAVCaptureOutput。( AVVideoWidthKey&を使用AVVideoHeightKey)。

例えば

NSDictionary* outputSettings = [movieFileOutput outputSettingsForConnection:videoConnection];
CGSize videoSize = NSMakeSize([[outputSettings objectForKey:AVVideoWidthKey] doubleValue], [[outputSettings objectForKey:AVVideoHeightKey] doubleValue]);

更新:
別のアイデアは、プレビュー セッションの画像バッファーからフレーム サイズを取得することです。
AVCaptureVideoDataOutputSampleBufferDelegate メソッドを実装しcaptureOutput:didOutputSampleBuffer:fromConnection: ます (AVCaptureOutput のデリゲートを設定することを忘れないでください)。

- (void)captureOutput:(AVCaptureFileOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    if(imageBuffer != NULL)
    {
        CGSize imageSize = CVImageBufferGetDisplaySize(imageBuffer);
        NSLog(@"%@", NSStringFromSize(imageSize));
    }
}
于 2013-01-04T09:34:12.023 に答える