Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting cameraViewTransform dynamically for iOS 8

I am working on a camera app where I am supposed to show a camera inside enter image description here

I am using camView.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.0) ; but getting different results for different devices and a black bar in between tab bar and camera. If I change the value of scaling the camera is too zoomed up. Any help in this matter would be very 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];
}
like image 612
Deepesh Gairola Avatar asked Sep 27 '22 13:09

Deepesh Gairola


1 Answers

What you are trying to achive is not supported officially by Apple. From the UIImagePickerController Class Reference:

To use an image picker controller containing its default controls, perform these steps:

...

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

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

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

Solution

To achieve what you want you have to use AVFoundation and not UIImagePickerController. AV Foundation library offers a much more powerful way of taking photos, which lets you put the camera view inside your own app. You can find a good tutorial here, or a newer one here

like image 165
Istvan Avatar answered Sep 29 '22 06:09

Istvan