在iPhone中运行相机时使用UIAlertView

时间:2011-06-23 07:38:53

标签: iphone uialertview avcapturesession

当我的相机使用OpenCV检测到脸部时,我正试图触发AlertView。我设法进行人脸检测,可以成功输出NSLog。但是当我尝试用

触发警报视图时
NSLog(@"Face Detected");
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Face Detected" message:@"Do you really  want to try again?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];

[alert addButtonWithTitle:@"Yes"];

[alert show];
[alert release];

我可以看到警报视图有点被触发,因为屏幕变暗但我永远看不到警报视图......

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

删除[alert release]。您已在其上调用了autorelease

此外,您可以在初始化程序中集成[alert addButtonWithTitle:@"Yes"];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Face Detected" 
                                                message:@"Do you really  want to try again?" 
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel" 
                                      otherButtonTitles:@"OK", nil] autorelease];

答案 1 :(得分:1)

你在哪里打电话给这个?主线程还是辅助线程? 因为UIKit的东西应该总是在主线程上完成。

代码示例:

- (void)opencvFaceDetect
{
  // stuff before
  [self performSelectorOnMainThread: @selector(openAlertView) withObject:nil waitUntilDone:false];
  // stuff after
}

然后

- (void)openAlertView
{
  UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Face Detected" 
                                                message:@"Do you really  want to try again?" 
                                               delegate:self 
                                      cancelButtonTitle:@"Cancel" 
                                      otherButtonTitles:@"OK", nil] autorelease];
}