2011-08-18 124 views
2

我有这样的代码: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提供了很多帮助!

+0

您的drawRect方法有效吗? – Akshay

+0

您没有调用initWithFrame,所以没有框架集。 – progrmr

+0

progrmr是正确的 - 您在viewDidLoad中创建的imageBackground具有一个原点为{0,0}且大小为{0,0}的框架。您需要在绘制之前设置视图的框架。 – glorifiedHacker

回答

7

您需要子类的UIView(或它的一个子类),并重写drawRect方法:

- (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]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, darkColor); 
    CGContextFillRect(context, CGRectMake(18, 62, 66, 66)); 
} 

为了进入,你可以得出一个有效的图形上下文。来自Apple的documentation on UIView

视图绘制按需进行。当第一次显示视图时, 或由于版面更改而全部或部分视图变为可见时,系统会要求视图绘制其内容。对于使用UIKit或Core Graphics包含 自定义内容的视图,系统会调用 视图的drawRect:方法。此方法的实现为 ,负责将视图的内容绘制到当前图形 上下文中,该上下文在系统调用 之前由系统自动设置。这会创建一个视图内容的静态可视化表示,然后可以在屏幕上显示该内容。

+0

好吧,我在我的视图中添加了drawRect方法,这是UIViewController的一个子类,但它似乎没有什么区别。有什么我必须先做的?我会检查你链接到的文件。 – Cole

+0

视图不是UIViewController的子类。为了在视图中进行自定义绘图,您需要继承UIView(而不是UIViewController)并覆盖UIView的drawRect:方法。 – glorifiedHacker

+0

好的,所以我创建了一个新类,它是UIView的子类。但是,如何告诉它,我希望在加载视图时调用drawRect? – Cole

1

最简单的方式,没有子类化,是整个代码的过程。

void DrawRectFrame(CGContext c, CGRect rc) 
{ 
    CGContextMoveToPoint(c,  rc.origin.x,     rc.origin.y); 
    CGContextAddLineToPoint(c, rc.origin.x+rc.size.width, rc.origin.y); 

    CGContextMoveToPoint(c,  rc.origin.x+rc.size.width, rc.origin.y); 
    CGContextAddLineToPoint(c, rc.origin.x+rc.size.width, rc.origin.y+rc.size.height); 

    CGContextMoveToPoint(c,  rc.origin.x+rc.size.width, rc.origin.y+rc.size.height); 
    CGContextAddLineToPoint(c, rc.origin.x,     rc.origin.y+rc.size.height); 

    CGContextMoveToPoint(c,  rc.origin.x,     rc.origin.y+rc.size.height); 
    CGContextAddLineToPoint(c, rc.origin.x,     rc.origin.y); 
}