ios 相机预览未按预期工作(AVFoundation)

utugiqy6  于 2023-04-22  发布在  iOS
关注(0)|答案(1)|浏览(134)

我试图取代第一层与相机预览层,但它不显示任何模拟器与白色屏幕出现。

(void) setUpGreyLayer {
         
         // TODO : This layer will be replaced with camera preview layer
         self.firstLayer = [CALayer layer];
         self.firstLayer.frame = [UIScreen mainScreen].bounds;
         self.firstLayer.backgroundColor = [UIColor whiteColor].CGColor;
     
         //Adding the greyLayer on top of firstLayer
         self.greyLayer = [CALayer layer];
         self.greyLayer.frame = self.firstLayer.bounds;
         self.greyLayer.backgroundColor = [UIColor grayColor].CGColor;
         self.greyLayer.opacity = 0.5;
           
         // Create a mask layer with a clear rounded rectangle shape in the center
         self.maskLayer = [CAShapeLayer layer];
         CGRect maskRect = CGRectMake((CGRectGetWidth(self.greyLayer.bounds) - 200) / 2.0, (CGRectGetHeight(self.greyLayer.bounds) - 250) / 2.0 - 0.10 * CGRectGetHeight(self.greyLayer.bounds), 200, 250);
         CGFloat cornerRadius = 77.0;
         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:maskRect cornerRadius:cornerRadius];
         [maskPath appendPath:[UIBezierPath bezierPathWithRect:self.greyLayer.bounds].bezierPathByReversingPath];
         self.maskLayer.path = maskPath.CGPath;
     
         // Set the mask layer to the greyLayer
         self.greyLayer.mask = self.maskLayer;
         [self.view.layer addSublayer:self.firstLayer];
         [self.view.layer addSublayer:self.greyLayer];
         [self setUpCaptureButton];
     }

现在我的下一个行动计划是用相机预览层替换第一层。但是当我添加相机预览层时,模拟器只显示一个没有覆盖的空白白色屏幕,并且捕获按钮是否有preview_layer的任何特定属性,应该将其标记为选中/未选中以使灰色覆盖出现在顶部。
我试图取代第一层与相机预览层,但它不显示任何模拟器与白色屏幕出现。
我试图取代第一层与相机预览层,但它不显示任何模拟器与白色屏幕出现。

- (void) viewDidLoad {
    [super viewDidLoad];
    
    [self setUpGreyLayer];
    
        [self setUpCaptureSession];
        [self setUpPreviewLayer];
        [self setUpCaptureButton];
  }

- (void)setUpPreviewLayer {
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.previewLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:self.previewLayer];
}
(void) setUpGreyLayer {
         
         // TODO : This layer will be replaced with camera preview layer
         
>! self.firstLayer = [CALayer layer];
>! self.firstLayer.frame = [UIScreen mainScreen].bounds;
>! self.firstLayer.backgroundColor = [UIColor whiteColor].CGColor;
     
         //Adding the greyLayer on top of previewLayer
         self.greyLayer = [CALayer layer];
         self.greyLayer.frame = self.previewLayer.bounds;
         self.greyLayer.backgroundColor = [UIColor grayColor].CGColor;
         self.greyLayer.opacity = 0.5;
           
         // Create a mask layer with a clear rounded rectangle shape in the center
         self.maskLayer = [CAShapeLayer layer];
         CGRect maskRect = CGRectMake((CGRectGetWidth(self.greyLayer.bounds) - 200) / 2.0, (CGRectGetHeight(self.greyLayer.bounds) - 250) / 2.0 - 0.10 * CGRectGetHeight(self.greyLayer.bounds), 200, 250);
         CGFloat cornerRadius = 77.0;
         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:maskRect cornerRadius:cornerRadius];
         [maskPath appendPath:[UIBezierPath bezierPathWithRect:self.greyLayer.bounds].bezierPathByReversingPath];
         self.maskLayer.path = maskPath.CGPath;
     
         // Set the mask layer to the greyLayer
         self.greyLayer.mask = self.maskLayer;
>! [self.view.layer addSublayer:self.firstLayer];
         [self.view.layer addSublayer:self.greyLayer];
         [self setUpCaptureButton];
     }
h9vpoimq

h9vpoimq1#

为了确认我的理解是正确的,你有两个层,即。第一个CALayer(bgColor =白色),第二个CALayer(bgColor = gray),你想把这个灰色层放在白色层上。你正在设置蒙版,这样你就可以得到你想要的形状,你想在里面捕捉用户输入。对吗?
我认为预览层的目的是在特定的捕获会话中从输出设备捕获的输出。如果是这样的话,为什么需要替换层?有没有可能你可以在不需要任何替换的情况下提前设置预览层?关于出现的白色,你能检查一下这个问题背后的原因是否是你为第一层设置的背景颜色吗?

相关问题