iOS - 在相机上定位自定义叠加层(垂直对齐)。顶部黑色条的大小 [英] iOS - Positioning a custom overlay on camera (vertical alignment). Size of top black bar

查看:223
本文介绍了iOS - 在相机上定位自定义叠加层(垂直对齐)。顶部黑色条的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找以下问题的程序化解决方案:我想在相机(iOS)上绘制自定义叠加层。我希望它在摄像机输出视图的垂直中心。我已经完成了相对于屏幕中心绘制我的自定义视图,但没有绘制到相机图片。

I'm searching a programmatic solution to following problem: I want to draw a custom overlay on camera (iOS). And I want it to be vertically central on the camera output view. I've accomplished to draw my custom view centrically relatively to the screen, but not to the camera picture.

要做到这一点,我需要获得大小顶部黑条吧。我怎么才能得到它?

To do that, I need to get the size of the top black bar. How can I get it?

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

The size of top and bottom bars are not equal, that's why the picture I get has this annoying y-offset to the bottom.

注意结果pic的偏移量:

Notice the offset of the resulting pic:

推荐答案

好的家伙,我最终使用的是AVFoundation,它最终是更好的解决方案,因为它可以自定义。我有 self.fullScreenImageView self.previewLayer 作为类属性。您需要编写自己的代码来实现按钮和拍摄相机的代码。在这里,您可以使用照片的质量来获得最佳的尺寸/质量。我使用0.6,但你可以选择你想要的任何东西。

OK guys, I ended up using AVFoundation, which in the end was even better solution as it is customizable. I had self.fullScreenImageView and self.previewLayer as class properties. You need to write your own code implementing the buttons and the code of taking a camera shot. Here you can play around with the quality of the photo to get optimal size/quality. I used 0.6, but you can choose whatever you want.

@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];
}

这篇关于iOS - 在相机上定位自定义叠加层(垂直对齐)。顶部黑色条的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆