21

iPhone の画面全体を埋める VideoPreviewLayer を作成する方法について約 100 万のスレッドを読みましたが、何も機能しません。

これは私のプレビューレイヤーの初期化です:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // Choosing bigger preset for bigger screen.
    _sessionPreset = AVCaptureSessionPreset1280x720;
}
else
{
    _sessionPreset = AVCaptureSessionPresetHigh;
}

[self setupAVCapture];

AVCaptureSession *captureSession = _session;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = self.view;
previewLayer.frame = aView.bounds;
previewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
[aView.layer addSublayer:previewLayer];

それが私のsetupAvCaptureメソッドです:

  //-- Setup Capture Session.
_session = [[AVCaptureSession alloc] init];
[_session beginConfiguration];

//-- Set preset session size.
[_session setSessionPreset:_sessionPreset];

//-- Creata a video device and input from that Device.  Add the input to the capture session.
AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(videoDevice == nil)
    assert(0);

//-- Add the device to the session.
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if(error)
    assert(0);

[_session addInput:input];

//-- Create the output for the capture session.
AVCaptureVideoDataOutput * dataOutput = [[AVCaptureVideoDataOutput alloc] init];
[dataOutput setAlwaysDiscardsLateVideoFrames:YES]; // Probably want to set this to NO when recording

//-- Set to YUV420.
[dataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]
                                                         forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; // Necessary for manual preview

// Set dispatch to be on the main thread so OpenGL can do things with the data
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

[_session addOutput:dataOutput];
[_session commitConfiguration];

[_session startRunning];

別の AVCaptureSessionPresets および ResizeFit オプションを使用しようとしました。ただし、常に次のようになります。

http://imageshack.us/photo/my-images/707/img0013g.png/

または、これは previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; を使用する場合です。レイヤーのサイズをログに記録すると、正しい全画面サイズが返されます。

http://imageshack.us/photo/my-images/194/img0014k.png/

4

4 に答える 4

78

試す:

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession: self.session];
[captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

迅速な更新

let previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

スウィフト 4.0 アップデート

let previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
于 2014-10-22T17:20:16.983 に答える
6

誰かがこの問題を抱えている場合は、画面の境界を取るだけで済みます

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = UIScreen.main.bounds
    previewLayer.videoGravity = .resizeAspectFill
    camPreview.layer.addSublayer(previewLayer)
于 2020-02-08T01:07:57.127 に答える