UIImagePickerController的cameraViewTransform忽略了iOS 10 beta上的“缩放”和“翻译”

时间:2016-08-24 13:32:17

标签: ios objective-c uiimagepickercontroller ios10 xcode8-beta6

我一直在使用下面的代码来扩展我的UIImagePickerController的实时预览以填充整个屏幕。到目前为止,这完美地运作在几天之前,我在iPhone 5上安装了iOS 10 beta 7,它不再扩展。我可以在UIImagePickerController的视图底部看到黑色补丁。似乎cameraViewTransform忽略了CGAffineTransformMakeScaleCGAffineTransformMakeTranslation来电。

这是我启动相机控制器的方法。我已将“allowsEditing”和“showsCameraControls”都设置为“NO”,以便提供我自己的自定义叠加视图。

objImagePickerController =[[UIImagePickerController alloc] init];

objImagePickerController.delegate = self;
objImagePickerController.sourceType =UIImagePickerControllerSourceTypeCamera;
objImagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
objImagePickerController.allowsEditing = NO;
objImagePickerController.showsCameraControls= NO;

这是我用来缩放相机实时预览的内容。

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float screenHeight= MAX(screenSize.height, screenSize.width);
float screenWidth= MIN(screenSize.height, screenSize.width);

float cameraAspectRatio = 4.0 / 3.0;
float imageWidth = floorf(screenWidth * cameraAspectRatio);
float scale = ceilf((screenHeight / imageWidth) * 10.0) / 10.0;

objImagePickerController.cameraViewTransform= CGAffineTransformMakeScale(scale, scale);

这是我将相机视图添加为子视图而不是传统的模态演示方法的方式,以满足我自己的要求。

 [[[UIApplication sharedApplication] keyWindow]addSubview:objImagePickerController.view];

在iOS 10 beta 8上运行的iPhone 5s的屏幕截图

enter image description here

在iOS 8.2上运行的iPhone 5s的屏幕截图

enter image description here

从上面的屏幕截图中可以看出,cameraViewTransform不尊重iOS 10测试版中的CGAffineTransformMakeScale

其他人是否面临这个问题?这是iOS 10 beta OS中出现的一种非常奇怪的行为。我无法找到解决方法。请指教。

注意:: objImagePickerController是UIImagePickerController的一个实例。

6 个答案:

答案 0 :(得分:7)

奇怪的是,它只允许我们在演示文稿完成后转换它。

示例:

self.presentViewController(
        self.imagePicker,
        animated: true,
        completion: {
            let screenSize = UIScreen.mainScreen().bounds.size
            let ratio: CGFloat = 4.0 / 3.0
            let cameraHeight: CGFloat = screenSize.width * ratio
            let scale: CGFloat = screenSize.height / cameraHeight

            self.imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0)
            self.imagePicker.cameraViewTransform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, scale, scale)
        }
    )

此代码将转换相机视图以匹配屏幕尺寸。

请注意,这是一种解决方法。它有效,但用户会看到它在演示时调整大小。

答案 1 :(得分:5)

已回答here,此问题已在iOS 10.2中修复,您可以在再次展示相机之前使用cameraViewTransform属性。

答案 2 :(得分:2)

我通过在摄像机e AVCaptureSessionDidStartRunningNotification被提出后延迟设置cameraViewTransform来解决:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cameraIsReadyNotification:) name:AVCaptureSessionDidStartRunningNotification object:nil]; 

-

- (void)cameraIsReadyNotification:(NSNotification *)notification  
 {  
          dispatch_async(dispatch_get_main_queue(), ^{  
          float scale = ceilf((screenHeight / imageWidth) * 10.0) / 10.0;

          objImagePickerController.cameraViewTransform=CGAffineTransformMakeScale(scale, scale);
          });  
  }

答案 3 :(得分:1)

我在IOS 9.3中也遇到了同样的问题。这是我使用的代码

//transform values for full screen support
#define CAMERA_TRANSFORM_X 1
#define CAMERA_TRANSFORM_Y 1.12412    

        if (IS_IPAD)
            CGAffineTransformScale(objImagePickerController.cameraViewTransform, CAMERA_TRANSFORM_X,  CAMERA_TRANSFORM_Y);
        else if (IS_IPHONE_5_Land||IS_IPHONE_4_Land||IS_IPHONE_6_Land||IS_IPHONE_6_PLUS_Land)
        {
            objImagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
        }

希望这有帮助

答案 4 :(得分:1)

我在增强现实应用程序中遇到了同样的问题,最后使用 AVFoundation 框架代替 UIImagePickerController 解决了这个问题。似乎cameraViewTransform不再适用于iOS 10。

以下代码对我有用。将函数添加到UIViewController子类并调用它。

- (BOOL) initCamera {

    AVCaptureSession *captureSesion = [[AVCaptureSession alloc] init];
    if ([captureSesion canSetSessionPreset:AVCaptureSessionPresetHigh]) {
        [captureSesion setSessionPreset:AVCaptureSessionPresetHigh];
    } else {
        return false;
    }
    AVCaptureDevice *camera = nil;
    NSArray<AVCaptureDevice*>* devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    // Select back camera
    for (AVCaptureDevice *device in devices) {
        if ([device position]==AVCaptureDevicePositionBack) {
            camera = device;
            break;
        }
    }
    if (camera == nil) {
        // Back camera not found.
        return false;
    }

    AVCaptureStillImageOutput *imageOutput = [[AVCaptureStillImageOutput alloc]init];
    [imageOutput setOutputSettings: @{AVVideoCodecKey: AVVideoCodecJPEG}];
    AVCaptureDeviceInput *deviceInput = [[AVCaptureDeviceInput alloc]initWithDevice:camera error: nil];

    if (![captureSesion canAddInput:deviceInput] || ![captureSesion canAddOutput:imageOutput]) {
        return false;
    }
    [captureSesion addInput:deviceInput];
    [captureSesion addOutput:imageOutput];

    AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSesion];

    // "Aspect Fill" is suitable for fullscreen camera.
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    layer.frame = self.view.bounds;
    layer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;

    [self.view.layer addSublayer:layer];
    [captureSesion startRunning];

    return true;
}

最重要的是使用AVLayerVideoGravityResizeAspectFill。使用此配置,摄像机视图将填充容器视图,保持其原始高宽比。

不要忘记导入框架:)

#import <AVFoundation/AVFoundation.h>

答案 5 :(得分:0)

corrigédansla version ios 10.2 问题已在ios 10.2版本中解决