Apply CGAffineTransform prior to layer being displayed

前端 未结 1 1733
旧时难觅i
旧时难觅i 2020-12-20 09:05

I\'m trying to scale an AVCaptureVideoPreviewLayer so it displays a \"zoomed in\" preview from the camera; I can make this work using setAffineTransform:C

相关标签:
1条回答
  • 2020-12-20 09:43

    Two simple alternative approaches, one (better) for iOS 7.x and one for earlier versions of iOS:

    float zoomLevel = 2.0f;
    if ([device respondsToSelector:@selector(setVideoZoomFactor:)]
        && device.activeFormat.videoMaxZoomFactor >= zoomLevel) {
      // iOS 7.x with compatible hardware
      if ([device lockForConfiguration:nil]) {
        [device setVideoZoomFactor:zoomLevel];
        [device unlockForConfiguration];
      }
    } else {
      // Lesser cases
      CGRect frame = captureVideoPreviewLayer.frame;
      float width = frame.size.width * zoomLevel;
      float height = frame.size.height * zoomLevel;
      float x = (frame.size.width - width)/2;
      float y = (frame.size.height - height)/2;
      captureVideoPreviewLayer.bounds = CGRectMake(x, y, width, height);
    }
    
    0 讨论(0)
提交回复
热议问题