Pregunta

Some QR code scanner apps will show the result (URL or something) by a alert once they finish scanning, so I want to do the same thing and display the result (integer number) of my video processing by alert. My video processing function is a delegate method. I read a few examples of UIAlertView, but buttons are needed to trigger the alert. In my case, the alert need to be shown after calculating the variable result. But if I add the alert in my processImage function:

- (void)processImage:(cv::Mat&)image {
    int result;
    videoProcessing() {
    ...      
    result = 10
    }

    if (result == 10) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10" 
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}    

My app will be terminated because of error:

2014-04-17 11:11:02.189 DotReader[3813:1803] *** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit/UIKit-2935.137/Keyboard/UIKeyboardTaskQueue.m:388
2014-04-17 11:11:02.190 DotReader[3813:1803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.'
*** First throw call stack:
(0x183ab6950 0x18ffbc1fc 0x183ab6810 0x1845eedb4 0x186aa8fc0 0x186aa8eec 0x186aa8b50 0x186aa6588 0x186aa565c 0x186f811d0 0x186f81698 0x186b00c7c 0x186affa04 0x186f83010 0x10007ad0c 0x10013c374 0x1827b8434 0x190594014 0x190593fd4 0x19059a4a8 0x1905964c0 0x19059b0f4 0x19059b4fc 0x1907296bc 0x19072954c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Can anyone tell me how to add UIAlertView correctly?

¿Fue útil?

Solución 2

I think that you are calling the alert in the thread operation. Try the code below:

-(void)processImage:(cv::Mat&)image {
    int result;
    videoProcessing(){
    ...      
    result = 10
    }

    if(result == 10){
        [self performSelectorOnMainThread:@selector(showAlertMessage) withObject:nil waitUntilDone:YES];
    }
}   

-(void)showAlertMessage{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10" 
        delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
} 

Otros consejos

All UI events should be called from the main thread

if (result == 10) {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    });

}

Try this:

-(void)processImage:(cv::Mat&)image {
    int result;
    videoProcessing() {
        ...      
        result = 10
    }

    if (result == 10) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"The code is" message:@"10" 
            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }];
    }
}

Hope this helps... :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top