我不知道这个问题是关于自动布局还是我做错了什么
我内部有一个UIViewController
和一个UIView
我正在使用AVCaptureSession
将相机视图放入其中
问题是,当我在摄像机视图中加载视图时,它不会填充该视图,因此左右两侧都有间隙。
我要做的是用相机填充整个UIView
这是我的代码:
...
@IBOutlet weak var camView: UIView!
var previewLayer : AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
...
previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.frame = self.camView.layer.bounds
self.camView.layer.addSublayer(previewLayer)
session.startRunning()
答案 0 :(得分:16)
您需要在布置子视图后进行设置。在viewDidLayoutSubviews
-(void)viewDidLayoutSubviews
{
[self setupAVCapture];
}
- (void)setupAVCapture {
NSError *error = nil;
AVCaptureSession *session = [AVCaptureSession new];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
[session setSessionPreset:AVCaptureSessionPreset640x480];
else
[session setSessionPreset:AVCaptureSessionPresetPhoto];
// Select a video device, make an input
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if ([session canAddInput:deviceInput])
[session addInput:deviceInput];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
previewLayer.frame = self.camView.bounds;
[self.camView.layer addSublayer:previewLayer];
[session startRunning];
}
您可能发生的事情是,viewDidLoad
尚未准确设置界限,因为子视图尚未根据其约束进行布局。当您为previewLayer
中的viewDidLayoutSubviews
设置框架时,它将采用正确的方向。但要小心,你还需要调整旋转预览和不旋转预览,并修改我使用的代码。对不起它不是很快,但这不重要。只需将您的实例化设置移至viewDidLayoutSubviews
因此,对于您的代码,您应该执行以下操作:
override func viewDidLayoutSubviews() {
...
previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer.frame = self.camView.layer.bounds
self.camView.layer.addSublayer(previewLayer)
session.startRunning()
答案 1 :(得分:0)
如果您希望图层正确填充UIView,您也可以为它定义约束:
self.camView.layer.addSublayer(previewLayer)
self.camView.layer.layoutManager = CAConstraintLayoutManager.layoutManager();
self.camView.layer.addConstraint(CAConstraint.constraintWithAttribute(CAConstraintAttribute.MinX, relativeTo: self.camView.layer, attribute: CAConstraintAttribute.MinX, scale: 1, offset: 0));
self.camView.layer.addConstraint(CAConstraint.constraintWithAttribute(CAConstraintAttribute.MaxX, relativeTo: self.camView.layer, attribute: CAConstraintAttribute.MaxX, scale: 1, offset: 0));
self.camView.layer.addConstraint(CAConstraint.constraintWithAttribute(CAConstraintAttribute.Width, relativeTo: self.camView.layer, attribute: CAConstraintAttribute.Width, scale: 1, offset: 0));
self.camView.layer.addConstraint(CAConstraint.constraintWithAttribute(CAConstraintAttribute.Height, relativeTo: self.camView.layer, attribute: CAConstraintAttribute.Height, scale: 1, offset: 0));
session.startRunning()
答案 2 :(得分:0)
设置priviewLayer
约束,如下面的代码所示它将占据整个屏幕。
Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView
Trailing Space = 0 from mainView
希望这对你有所帮助。
答案 3 :(得分:0)
这在 Swift 4.2
中对我有用只需在viewDidLayoutSubviews()函数中将AVCaptureVideoPreviewLayer的实例框架设置为父视图的框架,如下所示:
public class TestModel : PageModel
{
[BindProperty]
public IFormFile UploadPOIF { get; set; }
[BindProperty, Required, EmailAddress, StringLength(256, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
public string Email { get; set; }
[BindProperty, Required]
public string FirstName { get; set; }
[BindProperty, Required]
public string LastName { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
await UploadPhoto();
}
return Page();
}
}
答案 4 :(得分:-2)
如果您使用的是自动布局,请执行以下操作:
希望这会对你有所帮助。