7

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 :)

Oliver
  • 23,072
  • 33
  • 138
  • 230
Eksperiment626
  • 985
  • 3
  • 16
  • 30

4 Answers4

1

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!

msgambel
  • 7,320
  • 4
  • 46
  • 62
  • unfortunately, this is not fixing the rotation problem. but thanks for the memory tip! – nicholjs Nov 16 '11 at 17:33
  • Can you post more code then? It must be something in your view controller, as there is nothing wrong with the above code. – msgambel Nov 16 '11 at 18:07
0

Some time its happened if you are using a old generation iphone that have current os for example you are having iphone 3G and you update its ios to ios5 then some of the app you installed can behave differently you can check your app to another device for rectify your problem.

B25Dec
  • 2,301
  • 5
  • 31
  • 54
  • ok then try this tutorial provided by apple to do your camera things. http://stackoverflow.com/questions/7763459/save-the-captured-image-with-the-overlay-image-to-the-photoalbum-in-iphone/7763483#7763483 – B25Dec Oct 21 '11 at 10:11
0

Make sure you set the view controller hierarchy, with mainWindow.rootViewController and [vc addChildViewController:]. This propagates orientation information down to where you need it.

Donovan Voss
  • 1,450
  • 13
  • 12
0

It looks like for my project this was happening because you have not written the shouldAutoRotateToInterface: method in your root view controller. The rotate message propagates all the way down to the root view controller's shouldAutoRotateToInterface delegate when UIImagePickerController is called. Your method should look like this:

- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

When I upgraded my project to iOS 5, I borrowed my root view controller from an iOS 3 project. iOS 3 did not automatically write this method in view controller classes Try it out and let me know.

nicholjs
  • 1,762
  • 18
  • 30