-(voID)drawRect:(NSRect)dirtyRect{ [[self window] setAllowsConcurrentVIEwDrawing:YES]; [self setCanDrawConcurrently:YES]; [NSThread detachNewThreadSelector:@selector(DrawText) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(DrawRectangle) toTarget:self withObject:nil]; [NSThread detachNewThreadSelector:@selector(DrawGradIEnt) toTarget:self withObject:nil]; //Wherease these functions DrawText,DrawRectangle and DrawGradIEnt performs their task as suggested by name. //In DrawText,DrawRectangle,and DrawGradIEnt lockFocus and unlockFocus is being // used for multithreaded drawing.}
当我从Xcode运行相同的程序时,它运行正常.输出如下所示.
但是当我从外面运行它时,有问题,输出如下所示.
首先,我想知道从二级线程中绘制是否正确?或者从辅助线程中提取的另一种方法是什么?
这个问题背后的原因是什么?
解决方法 Ken Aspeslagh对于从辅助线程中绘制有些不正确(他是正确的,这通常是一个坏主意).从我所看到的代码中你没有一个很好的用例来绘制辅助线程.你能解释一下你为什么要这样做吗?你自己已经发现了setCanDrawConcurrently:它明确地讨论了从后台线程调用drawRect:请注意,视图窗口必须将allowsConcurrentVIEwDrawing设置为YES才能使其正常工作(这是默认设置).
苹果自己的Cocoa Drawing Guide有一个关于从次要线程绘图的部分.我已经强调了一些我认为与你相关的部分.
The Application Kit maintains a unique graphics context for each window and thread combination. Because each thread has its own graphics context object for a given window,it is possible to use secondary threads to draw to that window. There are some caveats,however.
During the normal update cycle for windows,all drawing requests are sent to your application’s main thread for processing. The normal update cycle happens when a user event triggers a change in your user interface. In this situation,you would call the setNeedsdisplay: or setNeedsdisplayInRect: method (or the display family of methods) from your application’s main thread to invalIDate the portions of your vIEw that require redrawing. You should not call these methods from any secondary threads.
If you want to update a window or vIEw from a secondary thread,you must manually lock focus on the window or vIEw and initiate drawing yourself. Locking focus configures the drawing environment for that window’s graphics context. Once locked,you can configure the drawing environment,issue your drawing commands as usual,and then flush the contents of the graphics context to the window buffer.
In order to draw regularly on a secondary thread,you must notify the thread yourself. The simplest way to send regular notifications is using an NSTimer or NSAnimation object. For more information on how to animate content,see “Advanced Drawing Techniques.”
The Cocoa Threading Programming Guide也说:
If you want to use a thread to draw to a vIEw,bracket all drawing code between the lockFocusIfCanDraw and unlockFocus methods of NSVIEw
除此之外,GCD块调用可能是一种比NSThread更好的在后台执行小型 *** 作的方法.
dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_HIGH,0),^{ // you can put each of these calls in their own queue if you want [self DrawText]; [self DrawRectangle]; [self DrawGradIEnt];});
但是,这可能与您的问题无关;我之所以提到它只是因为我认为它会更好地为你提供GCD队列.
总结以上是内存溢出为你收集整理的objective-c – 在NSView中绘制的多个线程全部内容,希望文章能够帮你解决objective-c – 在NSView中绘制的多个线程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)