Setting CameraViewTransform dynamically for iOS 8

I am working on a camera application where I have to show the camera inside enter image description here

I am using camView.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.0) ; , but I get different results for different devices and a black bar between the tab and the camera. If I change the scaling value, the camera will be too large. Any help in this matter would be greatly appreciated.

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self prepareCamera]; } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:NO]; [self launchCamera]; } - (void)prepareCamera{ camView = [[UIImagePickerController alloc] init]; camView.delegate = self; camView.sourceType = UIImagePickerControllerSourceTypeCamera; camView.showsCameraControls = NO; [self resizeCameraView]; } - (void)resizeCameraView{ CGSize screenSize = self.view.bounds.size; // set the aspect ratio of the camera float heightRatio = 4.0f / 3.0f; // calculate the height of the camera based on the screen width float cameraHeight = screenSize.width * heightRatio; // calculate the ratio that the camera height needs to be scaled by float scale = screenSize.height / cameraHeight; // move the controller to the center of the screen camView.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0); // concatenate the scale transform camView.cameraViewTransform = CGAffineTransformScale(camView.cameraViewTransform, scale, scale); } - (void)launchCamera{ [camSubView addSubview:camView.view]; [camView viewWillAppear:YES]; [camView viewDidAppear:YES]; } 
+5
source share
2 answers

What you are trying to achieve is not officially supported by Apple. From the UIImagePickerController class reference :

To use an image selection controller that contains default controls, follow these steps:

...

  1. Introduce the user interface. On iPhone or iPod touch, do this by default (full-screen) by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image selection controller as the new view controller.

    On the iPad, if you specify the source type of UIImagePickerControllerSourceTypeCamera, you can present the default image picker (full screen) or using popover . However, Apple recommends that the camera interface display only full-screen .

What you can do is present the UIImagePickerController modally (full-screen) and add custom controls using the cameraOverlayView property.

Decision

To achieve what you want, you need to use AVFoundation, not UIImagePickerController. The AV Foundation library offers a much more powerful way to take photos, which allows you to put camera view in your own application. You can find a good tutorial here or a newer one here.

+3
source

The question is what do you want to have as a result. CGAffineTransformMakeScale (1.0,1.0) is the default scale - so it does not make any changes. The camera on all apple devices has a 4: 3 aspect ratio. In addition, I have to admit that viewing the camera by default has a size equal to the width of the screen with height, which leads to 4: 3 advancement - that’s why you always have different views on the camera .

If you want to look like a camera with a height equal to the height of the screen, you need to do some affine transformation. Here is an example:

 CGFloat cameraAspectRation = 4.0/3.0; CGFloat cameraViewStarterHeight = screenSize.width*cameraAspectRation; CGFloat cameraAdjustedYPosition = (screenSize.height - cameraViewStarterHeight)/2 - 20; CGFloat cameraScaleTransformCoef = screenSize.height/cameraViewStarterHeight; CGAffineTransform adjustedYPositionTransform = CGAffineTransformMakeTranslation(0, cameraAdjustedYPosition); CGAffineTransform adjustedScaleTransform = CGAffineTransformMakeScale(cameraScaleTransformCoef, cameraScaleTransformCoef); self.cameraViewTransform = CGAffineTransformConcat(adjustedYPositionTransform, adjustedScaleTransform); 

But in this way, as you saw, the camera view will be "enlarged." Hope this helps you understand what you have.

+1
source

Source: https://habr.com/ru/post/1233015/


All Articles