iPhone - 如何在视图中绘制某些内容

发布于 2024-10-16 22:08:44 字数 877 浏览 8 评论 0原文

我有这段代码:

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] = {
    1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
};
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(rgb);
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = theCell.backgroundView.bounds;                
CGPoint start = CGPointMake(rect.origin.x, 0);
CGPoint end = CGPointMake(rect.origin.x, rect.size.height/2);

CGContextDrawLinearGradient(context, gradientRef, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

我想知道如何将其绘制到指定视图中,并将参数传递给我的函数的剪切矩形。提示:我不介意drawRect,我不会子类化任何东西。

提示 2:我不想插入任何以后无法删除的图层。

提示 3:这段代码不会绘制任何我的眼睛可以看到的东西...... :-( 缺少图形端口?

提示 4:我想简单地更改背景颜色来删除绘制,然后就完成了。 ..

I'v got this piece of code :

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] = {
    1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
};
CGGradientRef gradientRef = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(rgb);
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = theCell.backgroundView.bounds;                
CGPoint start = CGPointMake(rect.origin.x, 0);
CGPoint end = CGPointMake(rect.origin.x, rect.size.height/2);

CGContextDrawLinearGradient(context, gradientRef, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);

And I wonder how I can make it draw into a designated view and a clipping rect passed in parameters to my function. Tip: I don't mind about drawRect, I'm not subclassing anything.

Tip 2: I don't want to insert any layers that I won't be able to remove later.

Tip 3: This piece of code does not draw anything that my eyes could see..... :-( Missing a graphic port ?

Tip 4: I'd like to erase the draw simply changing the background color, and it's done...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2024-10-23 22:08:44

CGContextRef context = UIGraphicsGetCurrentContext(); 将获取当前视图的图形上下文,因此如果您在 UIView 的 drawRect: 方法中调用它,它将进行绘制。

我不明白你的意思:

我不介意drawRect,我不介意
子类化任何东西

,但如果你想进行自定义绘图,你必须重写drawRect:方法或使用图层。要使用图层,您可以调用 CGContextRef context = CGLayerGetContext(theLayer); 而不是 CGContextRef context = UIGraphicsGetCurrentContext();。

好的,所以我查看了文档,它说您可以通过从 drawRectMethod 调用 UIGraphicsGetCurrentContext() 来获取 CGContextRef。它是这么说的:在 iOS 应用程序中,您设置一个要绘制的 UIView 对象,并实现 drawRect: 方法来执行绘制。在调用自定义的drawRect:方法之前,视图对象会自动配置其绘图环境,以便您的代码可以立即开始绘图。作为此配置的一部分,UIView 对象为当前绘图环境创建图形上下文(CGContextRef 不透明类型)。您可以通过调用 UIKit 函数 UIGraphicsGetCurrentContext 来获取此图形上下文。您可以使用函数 UIGraphicsPushContext 和 UIGraphicsPopContext 保存和恢复图形上下文。

您可以在想要在视图之外的其他位置绘制的情况下创建自定义图形上下文对象。例如,您可能想要捕获一系列绘图命令并使用它们来创建图像或 PDF 文件。要创建上下文,请使用 CGBitmapContextCreate 或 CGPDFContextCreate 函数。获得上下文后,您可以将其传递给创建内容所需的绘图函数。

创建自定义上下文时,这些上下文的坐标系与 iOS 使用的本机坐标系不同。原点不是位于绘图表面的左上角,而是位于左下角,并且轴指向上方和右侧。您在绘图命令中指定的坐标必须考虑到这一点,否则生成的图像或 PDF 文件在渲染时可能会出现错误。有关使用 CGBitmapContextCreate 和 CGPDFContextCreate 的详细信息,请参阅“创建位图图形上下文”和“创建 PDF 图形上下文”。

CGContextRef context = UIGraphicsGetCurrentContext(); will get the graphics context for the current view, so if you call this in the drawRect: method of a UIView it will draw.

I don't understand what you mean by:

I don't mind about drawRect, I'm not
subclassing anything

but if you want to do custom drawing you must either override the drawRect: method or use layers. To use layers you would call CGContextRef context = CGLayerGetContext(theLayer); instead of CGContextRef context = UIGraphicsGetCurrentContext();.

Ok, so I looked at the documentation and it says that you can get a CGContextRef by calling UIGraphicsGetCurrentContext() from the drawRectMethod. Here's what it says: In an iOS application, you set up a UIView object to draw to and implement the drawRect: method to perform drawing. Before calling your custom drawRect: method, the view object automatically configures its drawing environment so that your code can start drawing immediately. As part of this configuration, the UIView object creates a graphics context (a CGContextRef opaque type) for the current drawing environment. You obtain this graphics context by calling the UIKit function UIGraphicsGetCurrentContext. You save and restore graphics contexts using the functions UIGraphicsPushContext and UIGraphicsPopContext.

You can create custom graphics context objects in situations where you want to draw somewhere other than your view. For example, you may want to capture a series of drawing commands and use them to create an image or a PDF file. To create the context, you use the CGBitmapContextCreate or CGPDFContextCreate function. After you have the context, you can pass it to the drawing functions needed to create your content.

When creating custom contexts, the coordinate system for those contexts is different from the native coordinate system used by iOS. Instead of the origin being in the upper-left corner of the drawing surface, it is in the lower-left corner and the axes point up and to the right. The coordinates you specify in your drawing commands must take this into consideration or the resulting image or PDF file may appear wrong when rendered. See “Creating a Bitmap Graphics Context” and “Creating a PDF Graphics Context” for details on using CGBitmapContextCreate and CGPDFContextCreate.

白云悠悠 2024-10-23 22:08:44

我可以建议您查看 CAGradientLayer 并将其添加为视图的子图层吗?更简单,并且它将是硬件加速的,这对于表格单元格很重要。

部分示例从这里窃取:

http://tumbljack.com/post/188089679 /gpu-accelerated-awesomeness-with-cagradientlayer

#import <QuartzCore/QuartzCore.h>  // Also import this framework
......
CAGradientLayer grad = [CAGradientLayer layer];

UIColor *colorOne     = [UIColor colorWithHRed:1.0f Green:1.0f Blue:1.0f alpha:1.0f];
UIColor *colorTwo     = [UIColor colorWithHRed:0.0f Green:0.0f Blue:0.0f alpha:1.0f];

NSArray *colors =  [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
grad.colors = colors;

CGRect rect = theCell.backgroundView.bounds; 
rect.size.height = rect.size.height / 2;
grad.frame = rect;
[self.layer addsublayer:grad]

您可能需要稍微调整一下颜色,不确定渐变是否倾斜......

Might I recommend you look into the CAGradientLayer, and add it as a sublayer of your view? Lots simpler, and it will be hardware accelerated which matters for table cells.

Example stolen partly from here:

http://tumbljack.com/post/188089679/gpu-accelerated-awesomeness-with-cagradientlayer

#import <QuartzCore/QuartzCore.h>  // Also import this framework
......
CAGradientLayer grad = [CAGradientLayer layer];

UIColor *colorOne     = [UIColor colorWithHRed:1.0f Green:1.0f Blue:1.0f alpha:1.0f];
UIColor *colorTwo     = [UIColor colorWithHRed:0.0f Green:0.0f Blue:0.0f alpha:1.0f];

NSArray *colors =  [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
grad.colors = colors;

CGRect rect = theCell.backgroundView.bounds; 
rect.size.height = rect.size.height / 2;
grad.frame = rect;
[self.layer addsublayer:grad]

You may have to play with the colors a bit, not sure if you had the gradient tilted or not...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文