我有这样的代码:CGContextFillRect没有绘制一个矩形?
CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0 green:92.0/255.0 blue:136.0/255.0 alpha:1.0] CGColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, darkColor);
CGContextFillRect(context, CGRectMake(18, 62, 66, 66));
而我只是把我的观点的viewDidLoad方法,但我认为不看任何不同比以前。我试过让它变得非常大而明亮的红色,但它不是绘画。有我需要的其他初始化代码吗?
编辑:有给我的答案参加工作后,我现在在我的veiwdidload有这样的:
[self.view addSubview:[[[imageBackground alloc] init] autorelease]];
和imageBackground.m:
@implementation imageBackground
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.opaque = YES;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGColorRef darkColor = [[UIColor colorWithRed:21.0/255.0 green:92.0/255.0 blue:136.0/255.0 alpha:1.0] CGColor];
darkColor = [[UIColor colorWithRed:255.0 green:0.0 blue:0.0 alpha:1.0] CGColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, darkColor);
CGContextFillRect(context, CGRectMake(18, 62, 66, 66));
}
@end
Imagebackground.h子类的UIView:
#import <UIKit/UIKit.h>
@interface imageBackground : UIView
@end
虽然我仍然没有得到任何结果。编辑2:嗯,我自己解决了它,但后来实现了progrmr已经告诉我如何解决它。我不得不使用[self.view addSubview:[[[imageBackground alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]];
而不是简单的alloc init。
感谢glorifiedhacker提供了很多帮助!
您的drawRect方法有效吗? – Akshay
您没有调用initWithFrame,所以没有框架集。 – progrmr
progrmr是正确的 - 您在viewDidLoad中创建的imageBackground具有一个原点为{0,0}且大小为{0,0}的框架。您需要在绘制之前设置视图的框架。 – glorifiedHacker