我在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]
答案 0 :(得分:10)
captureVideoPreviewLayer.frame = self.vImagePreview.view.bounds;
这是罪魁祸首。
您需要添加videoGravity
(表示视频在播放器图层的边界内显示的方式。),并带有AVLayerVideoGravityResizeAspectFill
属性
然后分配bounds
和最后但并非最不重要的,
你需要提供x and y- coordinates that establishes the center of a rectangle.
CGRect bounds=self.vImagePreview.view.layer.bounds;
avLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
avLayer.bounds=bounds;
avLayer.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
答案 1 :(得分:0)
更改相机预览屏幕宽度并检查。