0
votes

I am using UIImagePickerController for capturing the photo in both portrait and landscape mode from camera, allowed orientation in my app is portrait & landscape. In portrait mode, my camera overlay is working fine. But with landscape mode, after capturing photo default camera overlay label "Preview" and use & retake action button and also the live image captured from the camera is coming in portrait mode. Should I need to use auto resizing. How can I set this full camera overlay view (2nd screenshot) in landscape mode?

Here is my code to present UIImagePickerController:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){
    NSLog(@"Camera not available");
    return;
}

imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:nil];

For orientation, here is my code for view controller class where i am using image picker controller..

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;  
}

here is my camera initial view, when my device is in landscape mode:

enter image description here

second view after capture photo from camera :

enter image description here

My view controller, where i am displaying image picker is only in portrait mode, but capturing photo from camera should work for portrait and landscape using image picker. i want that in landscape mode, my camera overlay (attached second screenshot) should display their Preview label bar in the bottom of the view. Is it possible with default camera overlay or i need to create custom camera overlay view? Please help me to fix it. It is major problem in my app.

Thanks.

2
I have the same problem. How did you solve the issue? - doxsi

2 Answers

0
votes

You can try below code to fixe your Uiimage, you might have to tweak it a little bit to get your own correct orientation,

- (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
       UIImage *originalImage = (UIImage*) [info  objectForKey:UIImagePickerControllerOriginalImage];

    //--> get the fixed orientation image
  UIImage *fixedImage = [self fixOrientation:originalImage];


 }


- (UIImage *)fixOrientation:(UIImage*)takenImage {
// --> check the correct orientation if yes return the same.
if (takenImage.imageOrientation == UIImageOrientationUp) return takenImage;

// --> if taken image orientation is wrong adjust the image as per the correct orientation.
CGAffineTransform transform = CGAffineTransformIdentity;

switch (takenImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, takenImage.size.width, takenImage.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, takenImage.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, takenImage.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
        break;
}

switch (takenImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, takenImage.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, takenImage.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
        break;
}

//--> draw the calculated image above
CGContextRef ctx = CGBitmapContextCreate(NULL, takenImage.size.width, takenImage.size.height,
                                         CGImageGetBitsPerComponent(takenImage.CGImage), 0,
                                         CGImageGetColorSpace(takenImage.CGImage),
                                         CGImageGetBitmapInfo(takenImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (takenImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:

        CGContextDrawImage(ctx, CGRectMake(0,0,takenImage.size.height,takenImage.size.width), takenImage.CGImage);
        break;

    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,takenImage.size.width,takenImage.size.height), takenImage.CGImage);
        break;
}

// return the new fixed image to display
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}

you can adjust your code to get the desired orientation you want.

0
votes

No you can't do that with "UIImagePickerController " kindly take a look at documentation.

Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

But you can use the Assets Library Framework to access the list of images and create your own image picker.

You can also try a git : ELCImagePickerController.

Happy Coding.