0
votes

I have a view controller and i have added UIImagePicker as subview. On changing device orientation from portrait to landscape, camera appears to be on half screen rather than full screen. the images in the camera is also rotated 90 degrees. I am supporting device orientation in my app. how to fix this camera orientation issue. Any help is appreciated.

//Code for presenting camera

- (id)init
{
    self = [super init];
    if (self) {

        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType =  UIImagePickerControllerSourceTypeCamera ;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        picker.allowsEditing = YES;
        self.cameraPicker = picker;
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );;
        //cameraPicker.view.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height );

        [self.view addSubview:cameraPicker.view];


    }

    return self;
}

//Code for supporting device orientation

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self setFramesForControls:toInterfaceOrientation];
}

-(void)setFramesForControls :(UIInterfaceOrientation)orientation
{

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );

    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
    }
}
2

2 Answers

0
votes

Try this its working fine

declare this in viewdidload method

    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType =  UIImagePickerControllerSourceTypeCamera ;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picker.allowsEditing = YES;

and on button click event do the following

[self presentModalViewController:picker animated:YES];

no matter to check device orientation;

0
votes

To prevent your image this code will help you-

Define these macros on the top of your ViewController class-

 #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

Then manage your delegate method in this way-

#pragma mark- Delegate methods of UIImagePickerController Class

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

 UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

  NSString *imgType =  [info objectForKey:UIImagePickerControllerReferenceURL];

    //NSLog(@"%@",imgType);

    imgType = [imgType lastPathComponent];

    NSData *imageData;

    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
    {
        // code here
         imageData = UIImagePNGRepresentation(pickedImage);
    }

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
    {
        // code here
          imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
    }


    NSError * error = nil;

    [imageData writeToFile:path options:NSDataWritingAtomic error:&error];

    if (error != nil)
    {
        NSLog(@"ERROR: %@", error);
        return;
    }

 [picker dismissViewControllerAnimated:YES completion: nil];



}

It will capture the images according to your requirement.