iOS - 在相机上放置自定义叠加层(垂直对齐)。顶部黑条的大小

iOS - Positioning a custom overlay on camera (vertical alignment). Size of top black bar

我正在寻找以下问题的编程解决方案:我想在相机上绘制自定义叠加层 (iOS)。我希望它在相机输出视图中垂直居中。我已经完成相对于屏幕而不是相机图片居中绘制我的自定义视图。

为此,我需要获得顶部黑条 的大小 。我怎样才能得到它?

顶部和底部条的大小不相等,这就是为什么我得到的图片底部有这个令人讨厌的 y 偏移。

注意结果图片的偏移量:

好的,我最终使用了 AVFoundation,它最终是更好的解决方案,因为它是可定制的。我有 self.fullScreenImageViewself.previewLayer 作为 class 属性。您需要编写自己的代码来实现按钮和拍摄相机的代码。您可以在这里调整照片质量以获得最佳效果 size/quality。我用的是0.6,你可以随便选

@import MobileCoreServices;
@import AVFoundation;

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view setBackgroundColor:[UIColor blackColor]];

    self.fullScreenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.fullScreenImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.fullScreenImageView setCenter:self.view.center];

        AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
        captureSession.sessionPreset = AVCaptureSessionPresetHigh;
        AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if (captureDevice) {
            NSError *error;
            AVCaptureDeviceInput *captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
            if (!error && [captureSession canAddInput:captureDeviceInput]) {
                [captureSession addInput:captureDeviceInput];
            }
        }

        AVCaptureStillImageOutput *stillImageOutput = [AVCaptureStillImageOutput new];
        [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG,
                                              AVVideoQualityKey : @(0.6)}];
        [captureSession addOutput:stillImageOutput];

        self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
        [self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        self.previewLayer.frame = self.fullScreenImageView.bounds;
        self.previewLayer.position = CGPointMake(CGRectGetMidX(self.fullScreenImageView.bounds), CGRectGetMidY(self.fullScreenImageView.bounds));
        [self.fullScreenImageView.layer addSublayer:self.previewLayer];
        [captureSession startRunning];

        CGRect circleRect = CGRectMake(0, (self.fullScreenImageView.bounds.size.height - self.fullScreenImageView.bounds.size.width) / 2, self.fullScreenImageView.bounds.size.width, self.fullScreenImageView.bounds.size.width);
        UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:circleRect];
        CAShapeLayer *ringLayer = [CAShapeLayer layer];
        ringLayer.path = circle.CGPath;
        ringLayer.fillColor = nil;
        ringLayer.strokeColor = [UIColor redColor].CGColor;
        ringLayer.lineWidth = 2.0;
        ringLayer.lineDashPattern = @[@5.0, @10.0];
        [self.fullScreenImageView.layer addSublayer:ringLayer];

        [self.navigationController setToolbarHidden:NO];
        [self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
        [self.navigationController.toolbar setTintColor:[UIColor whiteColor]];
        // Add here some buttons, which are standard UIBarButtonItems

    [self.view addSubview:self.fullScreenImageView];
}