我正在一个项目中,我必须尝试在给定的时间执行给定的方法(见自动模式),如果自动模式不成功后,我切换到手动模式。只是为了提醒你,我对Objective-C和iOS开发很陌生,所以错误可能很明显。Objective-C内存问题与performSelector和无限循环
到目前为止,这里是我做了什么:
-(void)viewDidLoad {
[self performSelector:@selector(autoMode) withObject:nil afterDelay:1.0];
[self performSelectorInBackground:@selector(switchManualMode) withObject:nil];
}
-(void)autoMode {
@autoreleasepool {
while (isAutoMode == true) {
if ([session isRunning])
[self captureImage];
// NSLog(@"test");
}
}
}
-(void)switchManualMode {
[NSThread sleepForTimeInterval:2.0f];
isAutoMode = false;
self.button.enabled = true;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Changement de mode"
message:@"Du a un délai trop long, la capture automatique va être desactivée. La capture se fait maintenant de manière manuelle Touchez l'écran pour prendre une photo."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
我有几个问题在这里:
- 第一个是内存使用的峰值和应用程序崩溃。
- 第二个是switchManualMode永远不会被调用。
我试着只在日志中打印“测试”(而不是实际调用函数“captureImage”),当我这样做时,一切都很好。
我无法发布captureImage的代码,但该方法试图从AVCaptureSession获取图像,然后对其进行索姆运算处理。
无论如何,我不明白为什么switchManualMode被调用的情况下,而不是在其他。
几点注意事项:为什么'@ autoreleasepool'?您必须指定'[alert show]'应该在主线程中执行 - 目前您正在使用后台线程。一遍又一遍地捕捉图像只会导致极高的CPU和内存使用量。 – luk2302
我添加了@autoreleasepool,因为这是我在寻找解决方案释放无限循环时发现的。当我用NSLog替换captureImage的时候,[alert show]完美的工作。 – Chris
它不能正常工作,它不确定在什么时间点实际显示警报视图。如果它现在适合你:很好。它不会继续。 – luk2302