gpt4 book ai didi

iphone - IOS 开发,使用 UIImagePickerController 并按下使用会使应用卡住(不崩溃)

转载 作者:行者123 更新时间:2023-12-01 18:32:16 24 4
gpt4 key购买 nike

任何人都可以帮我解决这个问题吗?

在我的最后一个问题中,我使用的是带有 3 个选项卡项的 tabBarController。第三个选项卡有一个 uiViewController,里面有一个 UIImagePickerController(一个相机)。

现在每件事都在工作,除了一件事。当用相机拍摄图像并按“使用”时,我收到图像已保存的警报,我可以在相册中看到它(如果我关闭应用程序并查看它)但应用程序卡在这个位置我不能再做任何事情了。我可以在屏幕上看到图像,并且“使用”和“重拍”按钮不可用。就这样卡住了。

谁能看到我在这里做错了什么?

附言。在所有示例和教程中,我发现取消选择器中有一个版本......(也在我的代码中)。在我的例子中,选择器是 View Controller (imgPicker)的一个属性,我像往常一样在 dealloc 方法中释放它,这是写还是错?我应该这样生活还是我在这里做坏内存(我在这里没有得到任何“坏内存错误”,但这可能是我的错误......)?

我在 viveWillAppear 委托(delegate)方法中加载 UIImagePicker。
每件事都在同一个 TakePhotoViewController.m 文件中......

-(void) viewWillAppear:(BOOL)animated{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}

和委托(delegate)方法:
#pragma mark -
#pragma mark - UIImagePicker delegate methods

//saving the image that was taken
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

[picker release];
}

//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;

// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}

//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[self.tabBarController setSelectedIndex:0];
}

非常感谢你,
埃雷兹

最佳答案

您在取消操作上处于正确的轨道上,但在其他操作中未能做到这一点:

您需要在每次操作后在 ViewController 上调用dismiss。

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

编辑:新链接 https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/uid/TP40007070

... However, if you set this property to YES, your delegate must dismiss the image picker interface after the user takes one picture or cancels the operation.



所以,如果需要拍摄多于1张图片,showCameraControls需要设置为NO,并且需要使用自己的CameraOverlayView。

另外,绝对不要在你的代表中释放选择器!当 ViewController 弹出 View 时,它会自动释放它(假设选择器没有被保留在其他地方)。经验法则,如果您不拥有(保留)它,请不要释放它。 (一旦 iOS5 ARC 普及,这个概念可能会被遗忘)

您可能应该在 ViewController 呈现后释放它。

iPhone - modalViewController release

编辑:更多代码来帮助
-(void) viewWillAppear:(BOOL)animated{
// No need to store... this is 1 use only anyway. Save memory, and release it when done.
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = NO;
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentModalViewController:imgPicker animated:YES];
[imgPicker release]; // Release this here, this will execute when modal view is popped.
}

代表:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

关于iphone - IOS 开发,使用 UIImagePickerController 并按下使用会使应用卡住(不崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7471114/

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