为什么当我使用 UIImagePickerController 时我的相机界面表现得很奇怪?

发布于 2024-12-11 01:56:53 字数 3368 浏览 0 评论 0原文

在我的应用程序中,我希望用户能够拍照或使用照片库中的一张照片。当用户单击我制作的按钮时,会弹出一个警报视图,用户可以选择拍摄新照片或从照片库中拍摄一张照片。这是我使用的代码:

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

        }

    }

}

问题是,如果用户想要拍摄新照片,则会弹出相机界面,然后如果旋转设备,界面将如下所示: 在此处输入图像描述

然后当用户将其旋转回来时,它突然看起来像这样: 在此处输入图像描述

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

任何想法将不胜感激:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫相离 2024-12-18 01:56:53

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

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

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

  3. 如果您创建一个 UIImagePickerController 并呈现它,然后没有引用它以稍后释放它,则会出现内存泄漏。我建议在界面中设置 UIImagePickerController ,并在 viewDidLoad 方法中设置它。尝试:

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

这应该可以清除内存泄漏,并缩短加载时间。希望有帮助!

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!

芯好空 2024-12-18 01:56:53

有时,如果您使用的是具有当前操作系统的老一代 iPhone,例如您使用的是 iPhone 3G,并将其 ios 更新到 ios5,那么您安装的某些应用程序的行为可能会有所不同,您可以将您的应用程序检查到另一台设备以进行纠正你的问题。

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.

奶茶白久 2024-12-18 01:56:53

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

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

浮生未歇 2024-12-18 01:56:53

对于我的项目来说,发生这种情况似乎是因为您没有在根视图控制器中编写 shouldAutoRotateToInterface: 方法。当调用 UIImagePickerController 时,旋转消息会一直传播到根视图控制器的 shouldAutoRotateToInterface 委托。您的方法应该如下所示:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文