我正在使用CATiledLayer作爲我的UIView的背景層,我已將其放入UIScrollView。在我的視圖的init方法中,我創建了繪製簡單線條的CGPathRef對象。當我試圖在drawLayer:inContext中繪製此路徑時,它在滾動/縮放時偶爾會與EXEC_BAD_ACCESS(很少)一起崩潰。CATiledLayer在繪製先前準備好的CGPath時崩潰
代碼是很簡單的,我使用的唯一標準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];
}
更新: 我已經noiced一種只存在於iOS 5的這個問題,它的工作原理罰款4.3
調試EXEC_BAD_ACCESS很簡單,當你打開NSZombieEnabled時,它會告訴你哪個被釋放的變量被調用。 – Wolfert 2011-12-16 12:03:45
但在這段代碼中,不可能對內存做一些壞事,無論如何它只發生在CATiledLayer和多線程中。 – 2011-12-16 13:04:13