• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在iOS上的后台线程绘图

用户头像
it1352
帮助1

问题说明

我有一个非常复杂的绘图逻辑视图(它是一个从GIS数据中提取的地图视图)。在主线程上执行此绘图会锁定UI并使应用程序无响应。我想将绘图移到背景线程,例如NSOperation。

I have a view with some very complex drawing logic (it's a map view that draws from GIS data). Doing this drawing on the main thread locks up the UI and makes the app unresponsive. I want to move away the drawing to a background thread with for example an NSOperation.

构造它的最佳方法是什么?

What is the best way to structure this?

我目前正在绘制一个关闭内存CGContext,然后将其转换为CGImageRef,我将其发送到视图以在主线程上进行blit。不幸的是,这会占用大量内存,似乎不再使用GPU加速,因为它的速度要慢得多。有没有办法从后台线程直接绘制到视图?我知道UIKit不是多线程安全的,但是在我进行绘图时可能有一些方法可以锁定视图吗?

I am currently drawing to an off memory CGContext and then convert that to a CGImageRef which I send to the view to blit on the main thread. Unfortunately this uses up a lot of memory and it seems that the GPU acceleration is no longer used as it is quite a bit slower. Is there some way of drawing directly to the view from a background thread? I know that UIKit isn't multi-thread safe but maybe there is some way of locking the view while I'm doing the drawing?

正确答案

#1

我从未在iPhone上遇到过这种情况,但在Mac上我曾遇到过类似的问题。

I have never came across such situation on iPhone but on Mac I had similar problem once.

    1. 使用 CGLayer 委派离线上下文的绘图活动。
    1. 尝试为当前NSRunLoop添加计时器并按时间间隔执行图形命令。看起来应该是这样......
  1. Use CGLayer to delegate drawing activity of offline contexts.
  2. Try add timer for current NSRunLoop and on timed interval execute your graphic commands. It should look something like this...

...

kRenderFPS 25.0 //This is Maximum value

renderTimer = [[NSTimer timerWithTimeInterval:(1.0 / (NSTimeInterval)kRenderFPS) target:viewobject selector:@selector(RenderUI:) userInfo:nil repeats:YES] retain];


[[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSDefaultRunLoopMode];


[[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSModalPanelRunLoopMode];


[[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSEventTrackingRunLoopMode];

//In view class
-(void)RenderUI:(id)param
{
    [self setNeedsDisplayInRect:[self bounds]];
}

这应该可以解决问题。

This should do the trick.

还尝试对流程进行采样并检查谁在消耗CPU。这将使UI响应迅速。

Also try to sample your process and check who is consuming the CPU. This will make UI responsive and very fast.

您提到的性能问题可能是由于其他原因造成的。尝试cpu示例流程。它将让您了解谁实际上正在使用CPU。

The performance problem you mentioned is can be due to something else. Try to cpu sample process. It will give you insight on who is actually taking CPU.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhfig
系列文章
更多 icon
同类精品
更多 icon
继续加载