简体   繁体   中英

Video camera view in videoViewController works on iPhone but not iPad

I have a bug in a Universal iOS app that does not display the camera view in the iPad frame. The view is offset to the left. The view sits perfectly in an iPhone, but not on iPad. The original code was written 2 years ago and I am wondering if there has been a change in iOS that is now missing from this section of code.

Here is what I have at the moment;

     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;

As per the problem , what I can suggest just add this line in viewDidLayoutSubviews() if your project support Auto Layout .

Code:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM