2013-05-14 57 views
2

我有一个绘制到屏幕的方法,对于我的应用程序有很多好处,除了它不起作用的小问题...。如何以编程方式绘制到iOS中的显示?

我有一个UIImageView小部件的iOS程序,我试图以编程方式绘制它,但它只是当我运行该程序时看起来黑色。这是我的头文件出口报关:

@interface TestViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIImageView *imageView; 

@end 

...这是我实现:

@implementation TestViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), YES, 0.0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGFloat colour[] = { 1, 0, 0, 1 }; 
    CGContextSetFillColor(context, colour); 
    CGContextFillRect(context, CGRectMake(0, 0, 400, 400)); 

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.imageView setNeedsDisplay]; 

    UIGraphicsEndImageContext(); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

TestViewController是视图控制器和imageView的委托为UIImageView插件的出口。我尝试在图像中绘制一个400 x 400的红色框并将该图像分配给小部件。我甚至称setNeedsDisplay为好措施。

我在做什么错? 谢谢!

回答

3

这些线的问题:

CGFloat colour[] = { 1, 0, 0, 1 }; 
CGContextSetFillColor(context, colour); 

删除它们。取而代之的是,设置填充颜色是这样的:

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 

原因您的问题是,你不能创造一个色彩空间。您需要拨打CGContextSetFillColorSpace,但您未能如愿。但只有在您使用CGContextSetFillColor时才需要。但它已被弃用,所以不要使用它。按照文档推荐使用CGContextSetFillColorWithColor。它为您处理色彩空间问题。

相关问题