如何调整我的UIButton以保持宽高比

时间:2016-03-11 06:29:07

标签: ios objective-c uiviewcontroller uibutton avcapturesession

我在故事板上创建了一个View Controller并添加到按钮中,

我在我的View Controller的视图中启动AVCaptureSession,

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

CALayer *viewLayer = self.view.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.view.layer.bounds;
[self.view.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];

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

[session addOutput:stillImageOutput];

我的问题是我的按钮被隐藏,因为预览图层占据整个屏幕我希望预览图层占据整个屏幕,但也希望显示捕获和关闭会话的按钮

我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

尝试使用以下命令在视图控制器视图层次结构上移动按钮:

[self.view sendSubviewToFront:button];

在您将captureVideoPreviewLayer添加为子图层后执行此操作。