我有一种在屏幕上绘图的方法,它对我的应用程序有很多好处,除了它不起作用的小问题......根本。
我有一个带有 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采取良好的措施。
我究竟做错了什么?谢谢!