我遇到了一些间歇性崩溃的应用程序的问题。下面的代码在UIView中,CATiledLayer作为其后备层:
- (UIBezierPath *)path
{
if(_path == nil){
_path = [UIBezierPath bezierPath];
CGFloat lineWidth = 5;
[_path setLineWidth:lineWidth];
[_path setLineJoinStyle:kCGLineJoinRound];
[_path setLineCapStyle:kCGLineCapRound];
[_path moveToPoint:CGPointMake(100, 100)];
[_path addLineToPoint:CGPointMake(200,200)];
[_path addLineToPoint:CGPointMake(150,200)];
[_path addLineToPoint:CGPointMake(50,400)];
_path closePath];
return _path;
}
return _path;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithRed:0.1 green:0.1 blue:1 alpha:0.45] setStroke];//sets stroke color in current context
[self.path stroke];
}
我收到以下错误代码:
Single stepping until exit from function _ZN2CG4Path15apply_transformERK17CGAffineTransform, which has no line number information.
错误发生时似乎没有任何模式。它似乎在某些时候出现滚动或缩放。我有时会在缩放/滚动时崩溃。有时我可以缩放和滚动一段时间直到它崩溃。
我知道在iOS4之前,UIKit不是线程安全的,不能与CATiledLayers一起使用。请参阅tech note我的问题(我认为)似乎是一个线程问题。当然UIKit不能受到责备?
答案 0 :(得分:4)
尝试制作path
属性atomic
。
此外,您应该将drawRect
修改为以下内容:
- (void)drawRect:(CGRect)rect
{
[[UIColor colorWithRed:0.1 green:0.1 blue:1 alpha:0.45] setStroke];//sets stroke color in current context
@synchronized(self) {
[self.path stroke];
}
}