为什么当我使用 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];
}
}
}
问题是,如果用户想要拍摄新照片,则会弹出相机界面,然后如果旋转设备,界面将如下所示:
然后当用户将其旋转回来时,它突然看起来像这样:
一个小问题是相机需要很长时间才能加载。
任何想法将不胜感激:)
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:
And then when the user rotate it back it suddenly looks like this:
A little side problem is that the camera takes a long time to load.
Any thoughts would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能需要考虑的一些事项:
将
wantsFullScreenLayout
属性设置为YES
将导致视图忽略状态栏。但由于您使用的是默认相机控件,状态栏会自动隐藏。这是图像底部出现 20 像素灰色区域的最可能原因。默认相机控件仅设计为纵向模式。由于您的第一张图像看起来像是您以某种方式旋转了屏幕,因此您应该查看您的代码(可能是
shouldAutoRotate
)并了解为什么要这样旋转视图。这应该可以解决横向图片中的缩放问题。如果您创建一个
UIImagePickerController
并呈现它,然后没有引用它以稍后释放它,则会出现内存泄漏。我建议在界面中设置 UIImagePickerController ,并在 viewDidLoad 方法中设置它。尝试:.h
.m
这应该可以清除内存泄漏,并缩短加载时间。希望有帮助!
A few things you might want to consider:
Setting the
wantsFullScreenLayout
property toYES
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.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.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 theUIImagePickerController
in the interface, and setting it up in theviewDidLoad
method. Try:.h
.m
This should clean-up the memory leaks, and improve the load time. Hope that Helps!
有时,如果您使用的是具有当前操作系统的老一代 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.
确保使用 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.
对于我的项目来说,发生这种情况似乎是因为您没有在根视图控制器中编写 shouldAutoRotateToInterface: 方法。当调用 UIImagePickerController 时,旋转消息会一直传播到根视图控制器的 shouldAutoRotateToInterface 委托。您的方法应该如下所示:
当我将项目升级到 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:
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.