【问题标题】:Video camera view in videoViewController works on iPhone but not iPadvideoView 控制器中的摄像机视图适用于 iPhone 但不适用于 iPad
【发布时间】:2016-01-27 01:29:39
【问题描述】:

我在一个通用 iOS 应用程序中有一个错误,它没有在 iPad 框架中显示相机视图。视图向左偏移。该视图在 iPhone 上非常完美,但在 iPad 上却不行。原始代码是 2 年前编写的,我想知道 iOS 中是否发生了现在这部分代码中缺少的更改。

这是我目前拥有的;

     self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
AVCaptureConnection *avcc = [self.preview connection];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  [avcc setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
else
    [avcc setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
CGRect rect = [[[self view] layer] bounds];
[self.preview setBounds:CGRectMake(0, 0, rect.size.height, rect.size.width)];
[self.preview setPosition:CGPointMake(CGRectGetMidY(rect), CGRectGetMidX(rect))];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

【问题讨论】:

  • CGRect rect = [[[self view] layer] bounds];,你能告诉我self.view是否全屏(使用整个宽度和高度)?
  • 你在哪里写这段代码?- viewDidLoad()?

标签: ios objective-c iphone ipad viewcontroller


【解决方案1】:

根据问题,如果您的项目支持自动布局,我建议您在viewDidLayoutSubviews() 中添加这一行。

代码:-

{
    CGRect bounds=self.view.layer.bounds;
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;  
    self.preview.bounds=bounds;
    self.preview.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
}

【讨论】:

  • 谢谢。问题解决了。交换了 rect.size.height 和 rect.size.width 声明以及 CGRectGetMidY(rect) 和 CGRectGetMidX(rect) 的语句顺序。还添加了 CGRect 边界代码。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多