Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my camera interface act weird when I use UIImagePickerController?

In my app I want the user to be able to take a picture or use one from the photo library. When the user clicks the button I made a alert view pops up at the user can choose between taking a new photo or one from the photo library. Here is the code I've used:

    - (void)PictureAlert:(id)sender {

    UIAlertView *AlertDialog;

    // Setting up AlertDialog.
    AlertDialog = [[UIAlertView alloc] initWithTitle:nil 
                                             message:nil 
                                            delegate:self 
                                   cancelButtonTitle:@"Cancel" 
                                   otherButtonTitles:@"Choose From Library", @"Take New Picture", nil];

    [AlertDialog show]; }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];

    if ([ButtonTitle isEqualToString:@"Choose From Library"]) {

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

            // Pick photo.
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

            [self presentModalViewController:picker animated:YES];


        } else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

            // Setting up AlertDialog.
            UIAlertView *AlertDialog;

            AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" 
                                                     message:@"Device does not support a photo library"  
                                                    delegate:self 
                                           cancelButtonTitle:@"Dismiss" 
                                           otherButtonTitles:nil];

            [AlertDialog show];

        }


    } else if ([ButtonTitle isEqualToString:@"Take New Picture"]) {

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            // Take new photo.
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.wantsFullScreenLayout = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentModalViewController:picker animated:YES];


        } else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            // Setting up AlertDialog.
            UIAlertView *AlertDialog;

            AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
                                                     message:@"Device does not support a camera"  
                                                    delegate:self 
                                           cancelButtonTitle:@"Dismiss" 
                                           otherButtonTitles:nil];

            [AlertDialog show];

        }

    }

}

The problem is that if the user wants to take a new picture the camera interface pops up, and then if you rotate the device the interface looks like this: enter image description here

And then when the user rotate it back it suddenly looks like this: enter image description here

A little side problem is that the camera takes a long time to load.

Any thoughts would be appreciated :)

like image 734
Eksperiment626 Avatar asked Oct 19 '11 23:10

Eksperiment626


1 Answers

A few things you might want to consider:

  1. Setting the wantsFullScreenLayout property to YES will cause the view to ignore the status bar. But since you are using the default camera controls, the status bar hides automatically. This is the most likely cause for the 20 pixel grey area on the bottom of the image.

  2. The default camera controls are designed to be in portrait mode only. Since your first image looks like you somehow rotated the screen, you should look into your code (probably shouldAutoRotate) and see why you are rotating the view like that. This should fix the problem of the zoom you are getting in your landscape picture.

  3. You will have memory leaks if you create a UIImagePickerController, present it, and then have no reference to it to release it later. I would recommend setting the UIImagePickerController in the interface, and setting it up in the viewDidLoad method. Try:

.h

@interface yourView:UIViewController <UIImagePickerControllerDelegate> {
  UIImagePickerController * picker;
}

.m

- (void)dealloc; {
  [picker release];
  [super dealloc];
}

- (void)viewDidLoad; {
  [super viewDidLoad];
  picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;
  picker.allowsEditing = YES;
  picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
  NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex];

  if([ButtonTitle isEqualToString:@"Choose From Library"]){
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
      picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
      [self presentModalViewController:picker animated:YES];
    }
    else{
      // Setting up AlertDialog.
      UIAlertView *AlertDialog;
      AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
                                               message:@"Device does not support a camera"  
                                              delegate:self 
                                     cancelButtonTitle:@"Dismiss" 
                                     otherButtonTitles:nil];
      [AlertDialog show];
      [AlertDialog release];
    }
  }
  else if([ButtonTitle isEqualToString:@"Take New Picture"]){
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
      picker.sourceType = UIImagePickerControllerSourceTypeCamera;
      [self presentModalViewController:picker animated:YES];
    }
    else{
      // Setting up AlertDialog.
      UIAlertView *AlertDialog;
      AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
                                               message:@"Device does not support a camera"  
                                              delegate:self 
                                     cancelButtonTitle:@"Dismiss" 
                                     otherButtonTitles:nil];
      [AlertDialog show];
      [AlertDialog release];
    }
  }
}

This should clean-up the memory leaks, and improve the load time. Hope that Helps!

like image 155
msgambel Avatar answered Nov 15 '22 07:11

msgambel