gpt4 book ai didi

objective-c - CGContextFillRect 不工作

转载 作者:行者123 更新时间:2023-11-28 19:15:08 26 4
gpt4 key购买 nike

这段代码似乎什么都不做。编译成功,没有错误。屏幕上没有绘制矩形。

- (void)viewDidLoad
{

[super viewDidLoad];

UIColor *reliantMagenta = [UIColor colorWithRed:208.0f / 255.0f green:27.0f / 255.0f blue:124.0f / 255.0f alpha:1];

CALayer *reliantCanvasLayer = [CALayer layer];

reliantCanvasLayer.frame = CGRectMake(0, 0, 640, 960);

[[[self view] layer] addSublayer:reliantCanvasLayer];

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect leftRect = CGRectMake(0, 0, 200, 300);

CGContextSaveGState(ctx);
CGContextSetFillColorWithColor(ctx, reliantMagenta.CGColor);

CGContextFillRect(ctx, leftRect);
CGContextRestoreGState(ctx);

}

我刚刚开始学习 Quartz,并且非常喜欢它。如果您想解释 UIView、CALayers、CGLayers 和上下文之间的关系,这也会有很大帮助,但不是必需的,只是难以理解正在发生的事情。

最佳答案

如果您开始使用 Quartz,那么您应该从 Quartz 2D Programming Guide 开始,它遍历了所有这些。您在这里的主要错误是 viewDidLoad 中没有可用的上下文。这种绘制一般在drawRect:中完成。您对 UIGraphicsGetCurrentContext() 的调用此时返回 NULL

看完编程指南,你可能会有更多的疑问,但这正是你应该开始学习自定义绘图的地方。

关于objective-c - CGContextFillRect 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com