iphone - 绘制先前准备好的 CGPath 时,CATiledLayer 崩溃

标签 iphone ios ipad catiledlayer cgpath

我使用 CATiledLayer 作为我的 UIView 的支持层,我将它放在 UIScrollView 中。在我的 View 的 init 方法中,我正在创建绘制简单线条的 CGPathRef 对象。当我试图在 drawLayer:inContext 中绘制这条路径时,它偶尔会在我滚动/缩放时与 EXEC_BAD_ACCESS 崩溃(很少)。

代码很简单,我只使用标准的 CG* 函数:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
        tiledLayer.levelsOfDetail = 10;
        tiledLayer.levelsOfDetailBias = 5;
        tiledLayer.tileSize = CGSizeMake(512.0, 512.0);

        CGMutablePathRef mutablePath = CGPathCreateMutable();
        CGPathMoveToPoint(mutablePath, nil, 0, 0);
        CGPathAddLineToPoint(mutablePath, nil, 700, 700);
        path = CGPathCreateCopy(mutablePath);
        CGPathRelease(mutablePath);
    }
    return self;
}

+ (Class) layerClass {
    return [CATiledLayer class];
}

- (void) drawRect:(CGRect)rect {
}

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
    CGContextFillRect(ctx, self.bounds);

    CGContextSetLineWidth(ctx, 5);

    CGContextAddPath(ctx, path);
    CGContextDrawPath(ctx, kCGPathStroke);
}

- (void)dealloc {
    [super dealloc];
}

更新:
我注意到这个问题只存在于 iOS 5 上,它在 4.3 上运行良好

最佳答案

尝试在自定义 MKOverlayView 上绘制缓存的 CGPath 对象时遇到了类似的问题。

崩溃可能是因为 CGPath 不能同时在多个线程上绘制 - 它是一个不透明的类(如文档中所述)包含指向其点数组中当前点的指针。两个或多个线程在绘制此数组时同时迭代此数组可能会导致未定义的行为和崩溃。

我通过将 CGPath 对象复制到每个绘图线程中来解决这个问题(包含在互斥锁中以防止不完整的复制):

//lock the object's cached data
pthread_mutex_lock(&cachedPathMutex);
//get a handle on the previously-generated CGPath (myObject exists on the main thread)
CGPathRef myPath = CGPathCreateCopy(myObject.cachedPath);
//unlock the mutex once the copy finishes
pthread_mutex_unlock(&cachedPathMutex);

// all drawing code here
CGContextAddPath(context, myPath);
...
...
CGPathRelease(myPath);

如果您担心在每个线程上进行复制的内存开销,您也可以直接在缓存的 CGPath 对象上工作,但互斥锁必须在整个绘制过程中保持锁定(这有悖于线程的目的)绘画):
//lock the object's cached data
pthread_mutex_lock(&cachedPathMutex);

//get a handle on the previously-generated CGPath (myObject exists on the main thread)
CGPathRef myPath = myObject.cachedPath;

// draw the path in the current context
CGContextAddPath(context, myPath);
...
...

//and unlock the mutex
pthread_mutex_unlock(&cachedPathMutex);

我将通过说我不是使用 Quartz 进行多线程绘图的专家来限定我的答案,只是这种方法解决了我场景中的崩溃问题。祝你好运!

更新:
现在我重新访问了这段代码,因为 iOS 5.1.0 已经发布,看起来问题的根本原因实际上可能是 iOS 5.0.x 中 Quartz 中的一个错误。在移除 CGPathCreateCopy() 和互斥调用的 iOS 5.1.0 上进行测试时,我没有看到在 iOS 5.0.x 上遇到的任何崩溃。
//get a handle on the previously-generated CGPath (myObject exists on the main thread)
CGPathRef myPath = myObject.cachedPath;

// all drawing code here
CGContextAddPath(context, myPath);
...
...
//drawing finished

由于我们可能会在一段时间内支持 iOS 5.0.x,因此将互斥锁保留在您的代码中(除了轻微的性能影响)或在绘图前简单地运行版本检查不会有什么坏处。

关于iphone - 绘制先前准备好的 CGPath 时,CATiledLayer 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8533656/

相关文章:

iPhone 开发者门户不接受我的 CSR

ios - 如何在 Swift 3 中使用 UnsafeMutablePointer?

iphone - 以编程方式找出谁对我们的 iPhone 应用程序给予了 5 星评级

iPhone 应用程序 - 为什么我的 CGContext 圆圈看起来像黑色方 block ?

ios - 为什么 EAAccessoryDidConnectNotification 会出现两次?

iphone - kCFNull 和 [NSNull null] 是否可以互换

iphone - 获取 kAudioSessionProperty_CurrentHardwareInputNumberChannels 时的 OSStatus 560161140

iphone - 在突出显示时更改 UIButton 边框颜色

javascript - HTML5 视频标签在 iOS 上不起作用,视频未加载

ios - 当应用程序在后台时获取用户位置。 IOS