Ios CGContextAddPath:无效的上下文0x0
一开始:不,CGRect不是空的(我搜索了整个网站,了解了各种可能的修复方法,但似乎到目前为止我还没有成功) RqButton.hIos CGContextAddPath:无效的上下文0x0,ios,button,Ios,Button,一开始:不,CGRect不是空的(我搜索了整个网站,了解了各种可能的修复方法,但似乎到目前为止我还没有成功) RqButton.h #import <UIKit/UIKit.h> @interface RqButton : UIButton @property (nonatomic) int c; @property (nonatomic) float r; @property (nonatomic) float g; @property (nonatomic) CGRect fr
#import <UIKit/UIKit.h>
@interface RqButton : UIButton
@property (nonatomic) int c;
@property (nonatomic) float r;
@property (nonatomic) float g;
@property (nonatomic) CGRect frame;
- (id)initWithFrame:(CGRect)frame c:(int)c;
@end
我不能忽略这个错误,因为我的应用程序有时会完全冻结。我需要做所有这些来定制我的按钮的外观。通过.xib将类应用于按钮非常好
这个
许多CG方法即将问世;CGContextDrawRadialGradient:剪辑;CGContextSetStrokeColorWithColor:CGContextSaveGState:CGContextSetLineWidth:[…]
提前多谢了你不能这样称呼自己。操作系统在绘图时调用drawRect方法,并且只有在为类设置了绘图环境之后才调用。这就是发生撞车的原因。
如果您想尽快强制绘图,请调用[self-setNeedsDisplay]。只需单击按钮即可完成绘制,如果我使用“原始”initWithFrame进行绘制,则一切正常。正当我必须设置“自己的”initWithFrame以便将参数传递给类时,一切都中断了。我应该把[自我设置需要显示]放在哪里?我试过了,但没有任何改进。您没有提供足够的代码来查看其他情况(例如,没有响应单击而调用的代码),但是您不应该从init方法调用drawRect,因为这肯定会导致您看到的上下文错误。您也不应该声明名为“frame”的属性,因为该类已经有一个属性(从UIButton>UIControl>UIView继承)。
#import "RqButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation RqButton
+ (RqButton *)buttonWithType:(UIButtonType)type
{return [super buttonWithType:UIButtonTypeCustom];}
- (id)initWithFrame:(CGRect)frame c:(int)c
{
self = [super initWithFrame:frame];
if (self)
{ self.r=c%16;
self.g=c/16;
[self drawRect:frame];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
float width = CGRectGetWidth(rect);
float height = CGRectGetHeight(rect);
UIColor *borderColor = [UIColor colorWithRed:0.05f green:0.05f blue:0.05f alpha:1.00f];
NSLog(@"%f",width);
CGFloat BGLocations[2] = { 0.0, 1.0 };
CGFloat BgComponents[8] = { self.r*0.33f, self.g*0.33f, 0.0f , 1.0,
0.00, 0.00, 0.00, 1.0 };
CGColorSpaceRef BgRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef bgRadialGradient = CGGradientCreateWithColorComponents(BgRGBColorspace, BgComponents, BGLocations, 2);
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, width, height) cornerRadius: 5];
[roundedRectanglePath addClip];
CGPoint startBg = CGPointMake (width*0.5, height*0.5);
CGFloat endRadius= 32;
CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation);
CGColorSpaceRelease(BgRGBColorspace);
CGGradientRelease(bgRadialGradient);
[borderColor setStroke];
roundedRectanglePath.lineWidth = 2;
[roundedRectanglePath stroke];
}
@end
int xz= buttons[x][y].frame.origin.x;
int yz= buttons[x][y].frame.origin.y;
buttons[x][y] = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, 30, 30) c:(int)51 ];
[buttons[x][y] addTarget:self action:@selector(actionPick:) forControlEvents:UIControlEventTouchDown];
buttons[x][y].tag = tag;
[self.view addSubview:buttons[x][y]];
Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context