gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:40:16 26 4
gpt4 key购买 nike

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

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

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

任何想法将不胜感激:)

最佳答案

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

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

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

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

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

关于iphone - 为什么当我使用 UIImagePickerController 时我的相机界面表现得很奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com