Why is my camera interface acting weird when I use UIImagePickerController?

In my application, I want the user to be able to take a picture or use it in a photo library. When the user clicks on the button, I made a warning pop-up window, and you can choose between recording a new photo or a photo from the photo library. Here is the code I 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 image, a camera window will appear, and then if you turn the device, the interface looks like this: enter image description here

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

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

Any thoughts would be appreciated :)

+7
source share
4 answers

A few things you might want to consider:

  • Setting the wantsFullScreenLayout property to YES causes the view to ignore the status bar. But since you use standard camera controls, the status bar is automatically hidden. This is the most likely reason for the 20-pixel gray area at the bottom of the image.

  • The default camera controls are for portrait mode only. Since your first image looks like you rotated the screen somehow, you should look into your code (perhaps shouldAutoRotate ) and see why you rotate the view like that. This should fix the zoom problem you get in your landscape image.

  • You will have a memory leak if you create a UIImagePickerController , introduce it, and then you will not reference it to free it later. I would recommend installing the UIImagePickerController in the interface and setting UIImagePickerController up in the viewDidLoad method. Try:

.h

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

wow

 - (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 clear memory leaks and improve load times. Hope this helps!

+1
source

For some time this happened if you use an old-generation iphone with current os, for example, you have iphone 3G and you upgrade ios to ios5, then some of the applications you installed may behave differently, you can check your application on another device to fix your problem.

0
source

Make sure you set the view manager hierarchy using mainWindow.rootViewController and [vc addChildViewController:]. This spreads orientation information to where you need it.

0
source

It seems like this was happening for my project because you did not write the shouldAutoRotateToInterface: method in the root view controller. The pivot message propagates all the way to the mustAutoRotateToInterface root view controller when the 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 the iOS 3 project. IOS 3 did not automatically write this method to the class controller class. Give it a try and let me know.

0
source

All Articles