gpt4 book ai didi

ios - 为 calayer 绘图添加委托(delegate)会使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 13:26:32 24 4
gpt4 key购买 nike

我在名为 PieChartView.m 的文件中编写了以下代码:

#pragma mark DrawingDelegate
@interface DrawingDelegate : NSObject
@end

@implementation DrawingDelegate

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
//nothing here yet...
}

#pragma mark PieChartView
@implementation PieChartView

@synthesize pie_layer;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
float pie_layer_width = 300.f;
float pie_layer_height = 160.f;
float pie_layer_PosX = 7.f;
float pie_layer_PosY = 20.f;

DrawingDelegate* layerDelegate = [[DrawingDelegate alloc] init];

pie_layer = [[CALayer alloc] init];

//when commenting the following assignment the app no longer crashes
pie_layer.delegate = layerDelegate;
pie_layer.bounds = CGRectMake( 0.f, 0.f, pie_layer_width, pie_layer_height );
pie_layer.backgroundColor = [[UIColor whiteColor] CGColor];
pie_layer.position = CGPointMake( pie_layer_width/2+pie_layer_PosX, pie_layer_height/2+pie_layer_PosY );
pie_layer.cornerRadius = 10.f;
CGFloat components[4] = { 0.0, 0.0, 0.0, 1.0 };

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef black = CGColorCreate( colorspace, components );
pie_layer.borderColor = black;
pie_layer.borderWidth = 1.f;
pie_layer.shadowRadius = 5.f;
pie_layer.shadowColor = black;

CGColorRelease( black );
CGColorSpaceRelease(colorspace);

pie_layer.shadowOffset = CGSizeMake( 0.f, 5.f ),
pie_layer.shadowOpacity = 0.75f;
[self.layer insertSublayer:pie_layer above:self.layer];
[pie_layer setNeedsDisplay];
}
return self;
}

奇怪的是,只要我执行“pie_layer.delegate = layerDelegate;”这一行,应用程序就会崩溃。崩溃看起来像给定的屏幕截图。

enter image description here

为什么会这样?我的代码有什么问题?

这是另一个更详细的堆栈跟踪: enter image description here

最佳答案

如果这是 ARC 并且委托(delegate)是 weak 那么绘图委托(delegate)将被释放,因为没有任何人拥有它。

按照约定,委托(delegate)是弱的,但需要保持对您创建的 DrawingDelegate 对象的 strong 引用。

关于ios - 为 calayer 绘图添加委托(delegate)会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com