xcode IOS开发,使用UIImagePickerController并按下使用使应用程序卡住(不会崩溃)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7471114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS development, using UIImagePickerController and pressing use make the app get stuck (not crash)
提问by Erez
Can any one help me with this problem?
任何人都可以帮助我解决这个问题吗?
As in my last question, I am using a tabBarController with 3 tab items. The 3rd tab is has a uiViewController with a UIImagePickerController in it(a camera).
和我的上一个问题一样,我使用的是带有 3 个选项卡项的 tabBarController。第三个选项卡有一个 uiViewController,里面有一个 UIImagePickerController(一个相机)。
now every things is working except from one thing. When take an image with the camera and pressing "use" i am getting the alert the the image was save and i can see it in the photo album (if i close the app and look at it) but the app get stuck at this poing and i can not do anything any more. I can see the image on the screen and the "use" and "retake" buttons are not usable. just stuck like that.
现在除了一件事之外,每件事都在起作用。当使用相机拍摄图像并按“使用”时,我收到警报,图像已保存,我可以在相册中看到它(如果我关闭应用程序并查看它),但应用程序卡在此状态我不能再做任何事情了。我可以在屏幕上看到图像,但“使用”和“重拍”按钮不可用。就这样卡住了。
Can any one see what am i doing wrong over here?
任何人都可以看到我在这里做错了什么吗?
ps. In all of the examples and tutorials i found there is a release of the picker in the cancel...(also in my code). The picker in my case is a property of the view controller (imgPicker) and i release it as always in the dealloc method, Is that write or wrong? should i live it like that or am i doing a bad memory thing over here (I am not getting any "bad memory error" over here but it might be my mistake...)?
附:在所有示例和教程中,我发现在取消...(也在我的代码中)中发布了选择器。在我的例子中,picker 是视图控制器 (imgPicker) 的一个属性,我像往常一样在 dealloc 方法中释放它,这是写的还是错误的?我应该这样生活还是我在这里做的记忆力不好(我在这里没有收到任何“记忆力差的错误”,但这可能是我的错误......)?
I load the UIImagePicker in the viveWillAppear delegate method. Every thing is in the same TakePhotoViewController.m file...
我在 viveWillAppear 委托方法中加载 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];
}
and the delegate methods:
和委托方法:
#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];
}
Thank you very much, Erez
非常感谢,埃雷兹
回答by Daryl Teo
You're on the right track with the Cancel Action, but failed to do it for the other actions:
您在取消操作的正确轨道上,但未能为其他操作执行此操作:
You need to call dismiss on ViewController after each action.
您需要在每次操作后在 ViewController 上调用dismiss。
... 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.
... 但是,如果您将此属性设置为 YES,则您的代理必须在用户拍摄一张照片或取消操作后关闭图像选择器界面。
So, if you need to take more than 1 picture, showCameraControls needs to be set to NO, and you need to use your own CameraOverlayView.
所以,如果需要拍摄1张以上的图片,showCameraControls需要设置为NO,并且需要使用自己的CameraOverlayView。
Also, definitely DO NOT release picker in your delegate! When the ViewController pops the view, it will release it automatically (Assuming that the picker is not being retained elsewhere). Rule of thumb, if you don't own (retain) it, don't release it. (This concept will probably be forgotten once iOS5 ARC is commonplace)
另外,绝对不要在您的委托中发布选择器!当 ViewController 弹出视图时,它会自动释放它(假设选择器没有被保留在其他地方)。经验法则,如果你不拥有(保留)它,不要释放它。(一旦iOS5 ARC普及,这个概念可能会被遗忘)
You should probably release the ViewController after it has been presented.
您可能应该在 ViewController 出现后释放它。
iPhone - modalViewController release
iPhone - modalViewController 发布
EDIT: more code to help
编辑:更多代码来帮助
-(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.
}
Delegate:
代表:
- (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];
}