gpt4 book ai didi

ios - 线程正在执行行,但无效

转载 作者:行者123 更新时间:2023-11-29 04:57:03 25 4
gpt4 key购买 nike

好吧,这很奇怪。我有一个多线程应用程序,线程在主视图中回调一些方法。我使用调试器单步执行,行已运行,但没有任何反应。流程如下:

我有AViewController,它使用callbackTarget(AViewController self)和UpdateScreen Selector参数调用Wrapper类W。 W 类打开 UIImagePickerController,抓取图像,然后传递到图像处理 (IP) 类,同时传递回调目标和选择器。然后 IP 类生成一个线程来处理图像。完成后,线程调用callbackTarget.Selector,它用AViewController 中的结果更新 View 。

我在 UpdateScreen 中有一个断点。所有行都被执行,但屏幕上没有任何反应。我怀疑某些变量在线程之间不可见,但我不知道如何使其工作。请帮忙?

编辑添加示例代码。这很困惑,这就是为什么我一开始就没有包含它

代码片段

    //    AViewController, this is entry point
-(IBAction) callCardScanner_tapped{
[testResultLabel setText:@"ocr started"]; // this is shown
CardScaner* scanner = [[CardScaner alloc] init] ;
[scanner scanWithCameraSendResultTo:self selector:@selector(updateScreenWithResultFromCardScanner:)];
[scanner release];
}

//this method is used to pass into the thread so it can call back
-(void) updateScreenWithResultFromCardScanner:(OCRResult *)result{
// ... update labels with result
// ...
[resultLabel setText: result.resultString]; // these lines seem to be executed by main thread according to debugger, but the screen remain blank
}

CardScanner 包装类

-(void) scanWithCameraSendResultTo:(NSObject*) target selector :(SEL) selector{
// ...
UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[rootViewController presentModalViewController:imagePicker animated:true];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// ...
ImageProcessor* recognizer = [[ImageProcessoralloc] init];
[recognizer ocrImageInThread:img callbackTarget:callbackTarget selector:self.callbackMethod];
[imagePicker release];
}

图像处理器类

    - (void) ocrImageInThread:(UIImage*) photo callbackTarget:(NSObject*) target selector:(SEL) selector{
NSArray* args = [NSArray arrayWithObjects:photo, target, NSStringFromSelector(selector), nil];
NSThread* ocrThread = [[[NSThread alloc] initWithTarget:self selector: @selector(ocrImageThread_start:) object:args] autorelease];
[ocrThread start];

}

-(void) ocrImageThread_start:(NSArray*) args{
//.. do image processing to acquire result object, here in the end invoke the callback method
[callbackTarget performSelectorOnMainThread:NSSelectorFromString(selector) withObject:resultObject waitUntilDone:NO];
[resultObject release];
[pool release];

}

最佳答案

处理完成后,您必须更新线程上的 View 。大多数 UIKit 都不是线程安全的,只有在主线程上处理它才能保证工作。

您可以使用performSelectorOnMainThreaddispatch_async函数系列将工作转移回主线程。

关于ios - 线程正在执行行,但无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7751736/

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