当我使用UIImagePickerController时,为什么我的相机界面会很奇怪?

时间:2011-10-19 23:46:08

标签: iphone interface camera uiimagepickercontroller

在我的应用中,我希望用户能够拍照或使用照片库中的照片。当用户点击按钮时,我弹出警报视图,用户可以选择拍摄新照片或照片库中的照片。这是我用过的代码:

    - (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];

        }

    }

}

问题在于,如果用户想要拍摄新照片,则会弹出相机界面,然后如果您旋转设备,界面将如下所示: enter image description here

然后当用户将其旋转回来时,突然看起来像这样: enter image description here

一个小问题是相机需要很长时间才能装入。

任何想法都将被赞赏:)

4 个答案:

答案 0 :(得分:1)

您可能需要考虑的一些事项:

  1. wantsFullScreenLayout属性设置为YES将导致视图忽略状态栏。但由于您使用的是默认摄像头控件,状态栏会自动隐藏。这是图像底部20像素灰色区域的最可能原因。

  2. 默认的相机控件仅设计为纵向模式。由于您的第一张图片看起来像是以某种方式旋转了屏幕,因此您应该查看代码(可能是shouldAutoRotate)并查看为什么要像这样旋转视图。这应该可以解决您在风景画中遇到的变焦问题。

  3. 如果您创建UIImagePickerController,将其显示,然后没有引用它以便稍后释放,则会发生内存泄漏。我建议在界面中设置UIImagePickerController,并在viewDidLoad方法中进行设置。尝试:

  4. <强>·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];
        }
      }
    }
    

    这应该清理内存泄漏,并改善加载时间。希望有帮助!

答案 1 :(得分:0)

如果您使用的是具有当前操作系统的老一代iPhone,例如您正在使用iphone 3G并且将其ios更新为ios5,那么有些时候会发生这种情况,那么您安装的某些应用程序可能会有不同的行为,您可以将应用程序检查到另一个解决问题的装置。

答案 2 :(得分:0)

确保使用mainWindow.rootViewController和[vc addChildViewController:]设置视图控制器层次结构。这会将方向信息传播到您需要的位置。

答案 3 :(得分:0)

看起来我的项目发生了这种情况,因为您还没有在根视图控制器中编写shouldAutoRotateToInterface:方法。调用UIImagePickerController时,旋转消息一直向下传播到根视图控制器的shouldAutoRotateToInterface委托。您的方法应如下所示:

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

当我将项目升级到iOS 5时,我从iOS 3项目借用了我的根视图控制器。 iOS 3没有在视图控制器类中自动编写此方法试试看并告诉我。