21

我阅读了大约一百万条关于如何使 VideoPreviewLayer 填充 iPhone 的整个屏幕但没有任何效果的线程......也许你可以帮助我,因为我真的被卡住了。

这是我的预览层初始化:

    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 回答
1
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
            DispatchQueue.main.async {
                self.videoPreviewLayer?.frame = self.captureImageView.bounds
            }
            cropImageRect = captureImageView.frame
            videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
            videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
            captureImageView.layer.addSublayer(videoPreviewLayer!)
            session!.startRunning()

这对我来说是工作。我面临的问题是,我将以下行放在主线程之外。它创造了我认为的问题。

self.videoPreviewLayer?.frame = self.captureImageView.bounds

于 2021-12-28T06:34:33.663 回答
0

Swift 5 和 Xcode13

这对我有用

创建 AVCapturePreviewLayer

    class ViewController: UIViewController {
         let previewLayer = AVCapturePreviewLayer()
    }

将 PreviewLayer 添加到视图中

    override func viewDidLoad() {
           super.viewDidLoad()
    // Adds Element to the View Layer
           view.layer.addSublayer(previewLayer)

使 PreviewLayer 成为 View 的完整边界

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    previewLayer.frame = view.bounds 
于 2022-02-22T23:13:21.320 回答