Search code examples
ioscamerarotationuiimagepickercontrollerautorotate

Disable camera rotation in iOS?


My code to add a UIImagePickerController:

picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.showsCameraControls = NO;
    [self displayContentController:picker inView:cameraBoxView];
    [self.view sendSubviewToBack:picker.view];

- (void)displayContentController:(UIViewController *)content inView:(UIView *)v {

    //Delete the previous instance of the same view controller
    for (UIViewController *vc in self.childViewControllers) {

        if ([vc isKindOfClass:[content class]]) {

            [vc removeFromParentViewController];
        }
    }

    [self addChildViewController:content];
    content.view.frame = v.frame;
    [self.view addSubview:content.view];
    [content didMoveToParentViewController:self];

    if ([content isKindOfClass:[CCInnerNavigationController class]]) {

        innerNavigationController = (CCInnerNavigationController *)content;
    }
}

I have disabled all the device orientations except of portrait. But the image from camera is rotated. How to solve this issue in both iOS 6 and 7.

P.S. I understand that there are a lot of soludions but it seems they are too old because NOTHING of them works.


Solution

  • I changed the following code:

    imgView.image = img;
    

    to:

    imgView.image = [UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationRight];
    

    And now it works in both iOS 6 and 7. I don't know how it fixes this bug but it really works. Maybe image picker corrupts the image or image view and this code fixes it by creating a new image instance.