在drawRect中优化CGContextDrawRadialGradient: [英] Optimize CGContextDrawRadialGradient in drawRect:

查看:92
本文介绍了在drawRect中优化CGContextDrawRadialGradient:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPad应用程序中,我有一个UITableView,每次选择一个新的单元格时都会分配/初始化一个UIView子类。我已经在此UIView中重写了 drawRect:来绘制径向渐变,它可以正常工作,但是性能却受到了影响-当点击一个单元格时,UIView需要更长的绘制时间以编程方式实现渐变,而不是使用.png作为背景。有什么方法可以缓存我的 drawRect:方法或其生成的渐变以提高性能吗?我宁愿使用 drawRect:而不是.png。我的方法如下:

In my iPad app, I have a UITableView that alloc/inits a UIView subclass every time a new cell is selected. I've overridden drawRect: in this UIView to draw a radial gradient and it works fine, but performance is suffering - when a cell is tapped, the UIView takes substantially longer to draw a gradient programmatically as opposed to using a .png for the background. Is there any way to "cache" my drawRect: method or the gradient it generates to improve performance? I'd rather use drawRect: instead of a .png. My method looks like this:

- (void)drawRect:(CGRect)rect
{
     CGContextRef context = UIGraphicsGetCurrentContext();

     size_t gradLocationsNum = 2;
     CGFloat gradLocations[2] = {0.0f, 1.0f};
     CGFloat gradColors[8] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.5f}; 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
     CGColorSpaceRelease(colorSpace);

     CGPoint gradCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
     float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;

     CGContextDrawRadialGradient (context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);

     CGGradientRelease(gradient);
}

谢谢!

推荐答案

您可以将图形渲染到上下文中,然后将其存储为UIImage。 此答案应该让您开始:

You can render graphics into a context and then store that as a UIImage. This answer should get you started:


drawRect: UIView 用来绘制视图本身,而不是预先创建图形对象。

drawRect: is a method on UIView used to draw the view itself, not to pre-create graphic objects.

因为似乎要创建形状以存储它们并稍后绘制,将形状创建为 UIImage 并使用 UIImageView 绘制形状似乎是合理的。 UIImage 可以直接存储在 NSArray 中。

Since it seems that you want to create shapes to store them and draw later, it appears reasonable to create the shapes as UIImage and draw them using UIImageView. UIImage can be stored directly in an NSArray.

要创建图像,请执行以下操作(在主队列上;不在drawRect中:):

To create the images, do the following (on the main queue; not in drawRect:):

1)创建位图上下文

UIGraphicsBeginImageContextWithOptions(size, opaque, scale);

2)获取上下文

CGContextRef context = UIGraphicsGetCurrentContext();

3)绘制所需的任何东西

3) draw whatever you need

4)将上下文导出到图像中

4) export the context into an image

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

5)破坏上下文

UIGraphicsEndImageContext();

6)存储对图像的引用

6) store the reference to the image

[yourArray addObject:image];

为每个要创建的形状重复。

Repeat for each shape you want to create.

有关详细信息,请参见文档用于上述功能。为了更好地理解 drawRect:和程序中任意位置之间的绘制以及与上下文的一般使用,我建议您阅读 Quartz2D编程指南,特别是有关图形上下文。

For details see the documentation for the above mentioned functions. To get a better understanding of the difference between drawing in drawRect: and in arbitrary place in your program and of working with contexts in general, I would recommend you read the Quartz2D Programming Guide, especially the section on Graphics Contexts.

这篇关于在drawRect中优化CGContextDrawRadialGradient:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆