gpt4 book ai didi

ios - 相机预览不填满屏幕

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:13:26 30 4
gpt4 key购买 nike

这就是我在 iPad3 中以横向模式设置相机预览的方式

- (void)viewDidAppear:(BOOL)animated

{

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset640x480;

CALayer *viewLayer = self.vImagePreview.layer;

NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];

[session startRunning];

在 Storyboard中,我将 View 设置为填满整个屏幕,但它最终看起来像这样:

enter image description here

此外,图像也旋转了 90º 度。我该如何更改它,以便让相机预览填满整个屏幕?

谢谢。

最佳答案

旋转图层:

    switch (self.interfaceOrientation)
{
case UIInterfaceOrientationLandscapeRight:
[captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
break;
case UIInterfaceOrientationLandscapeLeft:
[captureVideoPreviewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI / 2)];
break;
}

[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

关于ios - 相机预览不填满屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706851/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com