开发者

iPhone - How to draw something in a view

开发者 https://www.devze.com 2023-02-10 08:10 出处:网络
I\'v got this piece of code : CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); CGFloat colors[] = {

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 design开发者_如何学Pythonated 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...


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号