我有一个应用程序正在石英上下文绘制线条。当用户在屏幕上移动他的手指时,该应用程序开始绘制。iphone - 主线程冻结了半秒钟......为什么?
当TouchesMoved被触发时,我将Quartz上下文保存到一个PNG文件中(我知道保存文件很慢......我试图对内存执行此操作,但是应用程序内存使用量猛增,所以我试图做到磁盘)。
根据上下文被保存到这一点,我做这在触摸移动
if (firstMove) // first movement after touchesbegan
[NSThread detachNewThreadSelector:@selector(newThreadUNDO)
toTarget:self
withObject:nil];
firstMove = NO
}
,然后我有
- (void) newThreadUNDO {
NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];
[NSThread setThreadPriority:0.1];
[NSThread sleepForTimeInterval:0.0];
[self performSelectorOnMainThread:@selector(copyUNDOcontext) withObject:nil waitUntilDone:NO];
[p release];
}
和
- (void) copyUNDOcontext {
CGFloat w = board.image.size.width;
CGFloat h = board.image.size.height;
CGRect superRect = CGRectMake(0,0, w, h);
CGSize size = CGSizeMake(w, h);
UIGraphicsBeginImageContext(size);
CGContextRef new = UIGraphicsGetCurrentContext();
// lineLayer is the layer context I need to save
CGContextDrawLayerInRect(new, superRect, lineLayer);
UIImage *imagem = UIGraphicsGetImageFromCurrentImageContext();
[self saveTempImage:imagem :@"UNDO.png"];
UIGraphicsEndImageContext();
}
的问题是:作为一旦用户开始移动,新线程就会被触发,但即使这个新线程的优先级较低,主线程仍然会冻结大约半秒(可能在文件被保存时)。
这是为什么?
我该如何解决这个问题?
谢谢。
谢谢!奇迹般有效! – SpaceDog 2010-08-06 11:24:04
很好用。将来你可能想创建一个线程池。这样您就不会重复分配发布池和线程。但是,iOS可能会在后台有一个线程池,将可用(或全局)线程返回给分配。我会在iOS 4上说更多,因为它有GCD。至少你会节省几个CPU周期。 请记住,触摸从另一个线程可可变成一个不舒服的情况。 – 2010-08-06 16:51:32