gpt4 book ai didi

ios - [非 A 类型保留] : message sent to deallocated instance, objective-c

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

我正在使用渐变自定义我的 View ,并通过执行以下操作在其上添加一个 uilabel :

@implementation ECertificateViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CustomViewBackGround *bgView = [[CustomViewBackGround alloc] initWithFrame:CGRectMake(0, 0, 301, 26)];
[self.mainView addSubview:bgView];
}


#import "CustomViewBackGround.h"

@implementation CustomViewBackGround
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CGFloat height = 20.0;
CGFloat x = 5;
CGFloat y = 3;
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, self.bounds.size.width - 2 * x, height)] ;
titleLabel.text = @"This is my label";
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize:14];
titleLabel.textColor = [UIColor lightGrayColor];
[self addSubview:titleLabel];
}
return self;
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
green:230.0/255.0
blue:230.0/255.0
alpha:1.0].CGColor;
CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;

CGRect paperRect = self.bounds;

// Fill with gradient
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);

// Add white 1 px stroke
CGRect strokeRect = paperRect;
strokeRect.size.height -= 1;
strokeRect = rectFor1PxStroke(strokeRect);

CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextSetLineWidth(context, 1.0);
CGContextStrokeRect(context, strokeRect);

// Add separator
CGPoint startPoint = CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
CGPoint endPoint = CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
draw1PxStroke(context, startPoint, endPoint, separatorColor);
}

当我在模拟器中运行时,一切正常,如我所料。但是,当我在设备上安装时,应用程序崩溃了,我得到了

[Not A Type retain]: message sent to deallocated instance,objctive c

您知道我为什么会遇到这个问题吗?

最佳答案

试试这个:

UIColor *whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
...
drawLinearGradient(context, paperRect.CGColor, whiteColor.CGColor, lightGrayColor.CGColor);

或者这个:

CFColorRef whiteColor = CFBridgingRetain([UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor);
...
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);
...
CFRelease(whiteColor);
...

可能 ARC 会在您不使用它们时立即释放您的 UIColor,并且通过释放它们,它们的 CGColor 属性也会被释放,因为没有一个保留了他们。

关于ios - [非 A 类型保留] : message sent to deallocated instance, objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792074/

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