我在 iPhone 屏幕的一半上添加了相机预览。
-(void) viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.view.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;
[self.vImagePreview.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];
}
我在 viewDidLoad 方法中添加了相机视图控制器并设置了它的框架:
vImagePreview.view.frame = CGRectMake(0, 44, 320, 179);
但是相机视图不是320。知道如何设置相机视图的宽度添加这种方式吗?
高度很好,但宽度约为 100 宽。
就像在这个链接上宽度是 320 但相机视图由于某种原因更小(宽度)。![在此处输入图像描述][1]