Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImagePickerController's cameraViewTransform is ignoring 'scaling' and 'translation' on iOS 10 beta

I have been using below code to scale my UIImagePickerController's live preview to fill the entire screen. This worked perfectly till now. Before few days, I installed iOS 10 beta 7 on an iPhone 5 and it doesn't scale anymore. I can see black patch at the bottom of UIImagePickerController's view. Seems like cameraViewTransform is ignoring the CGAffineTransformMakeScale and CGAffineTransformMakeTranslation calls.

This is how I initiate my camera controller. I have set both "allowsEditing" and "showsCameraControls" to 'NO' in order to provide my own custom overlay view.

objImagePickerController =[[UIImagePickerController alloc] init];

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

This is what I use to scale the camera live preview.

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);

This is how I add the camera view as a subview instead of traditional modal presentation method, to suit my own requirements.

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

screenshot from iPhone 5s running on iOS 10 beta 8

enter image description here

screenshot from iPhone 5s running on iOS 8.2

enter image description here

As noticeable from the above screenshots, the cameraViewTransform doesn't respect the CGAffineTransformMakeScale in iOS 10 beta.

Did anybody else face this issue? This is a really weird behavior appearing in iOS 10 beta OS. I am unable to find a workaround for this. Please advise.

NOTE:: objImagePickerController is an instance of UIImagePickerController.

like image 421
Rashmi Ranjan mallick Avatar asked Aug 24 '16 13:08

Rashmi Ranjan mallick


2 Answers

Strangely it only allows us to transform it after the presentation was completed.

Example:

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)
        }
    )

This code will transform the camera view to match the screen size.

Note that this is a workaround. It works, but the user will see it resizing upon presentation.

like image 176
Thomas Neuteboom Avatar answered Nov 19 '22 22:11

Thomas Neuteboom


As answered here, this issue has been fixed in iOS 10.2 and you can use the cameraViewTransform property before presenting the camera again.

like image 5
Hans Knöchel Avatar answered Nov 20 '22 00:11

Hans Knöchel