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:我想擦除绘图,只需更改背景颜色,就完成了……

4

2 回答 2

2

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

我不明白你的意思:

我不介意drawRect,我没有继承任何东西

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.

于 2011-02-13T00:25:29.480 回答
0

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...

于 2011-02-13T01:37:19.453 回答