The code below results in a pure red square:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
But the code below results in a white square when what I'd expect is a different shade of red. Can anyone tell me why this is?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor* boxColour = [UIColor colorWithRed:217 green:37 blue:70 alpha:1.0];
CGContextSetStrokeColorWithColor(context, boxColour.CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
The colorWithRed:green:blue:alpha: method of the UIColor class takes parameters in the range 0.0 to 1.0. You probably just need to divide all the values (except alpha) by 255.0f:
UIColor* boxColour = [UIColor colorWithRed:217/255.0f green:37/255.0f blue:70/255.0f alpha:1.0f];
Apple Documentation
Related
My app is drawing a shadow using the following code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGContextClipToMask(context, rect, CGBitmapContextCreateImage(context));
CGContextRestoreGState(context); // Warning shows in this line
}
When I run Product > Analyze, it marks the last instruction of this block with the message: "Potential leak of an object". When I remove that line, it shows the same message but in the closing bracket.
Any idea of what I'm doing wrong?
Thanks
I think it's the CGBitmapContextCreateImage that is leaking.
Try assigning it to a local variable and then calling CGImageRelease with it once you've clipped to mask.
Another iPhone - CGBitmapContextCreateImage Leak
chedabob's answer worked! Here's the final code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGImageRef bitmap = CGBitmapContextCreateImage(context);
CGContextClipToMask(context, rect, bitmap);
CGContextRestoreGState(context);
CGImageRelease(bitmap);
}
This piece of code works fine
self.fillColor = [UIColor whiteColor].CGColor;
While this throws an exception in my drawRect function
self.fillColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2].CGColor;
drawRect:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.fillColor); //exception here
CGContextSetStrokeColorWithColor(context, self.fillColor);
CGContextFillEllipseInRect(context, rect);
CGContextStrokeEllipseInRect(context, rect);
}
What's the issue?
Ok. It looks like the error is from the way the property is defined.
Try defining it this way...
#property UIColor *fillColor;
Then in drawRect you can use...
fillColor.CGColor
I have done this hundreds of times. Alpha values don't make any difference.
I have a UIColor initiated with [UIColor colorWithRed:Green:Blue:Alpha] and I want to extract its corresponding CGColor to use with CGContextSetFillColorWithColor.
I tried [[UIColor colorWithRed...] CGColor] but for some reason I get no coloring.I
[[UIColor colorWithRed:216 green:156 blue:1 alpha:1] CGColor]
The weird thing is that when I use:
[[UIColor orangeColor] CGColor]
It works just fine.
Any ideas?
Thanks!
EDIT:
The actual code:
CGRect rect = CGRectMake(0.0, 0.0, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:216 green:156 blue:1 alpha:1].CGColor);
CGContextFillRect(context, rect);
self.image = UIGraphicsGetImageFromCurrentImageContext();
I don't think its code-related - again, if I change the UIColor to a constant color it all works.
The purpose of the code is to create an UIImage filled with a specific color.
I am beginner with Core Garphics and I am try to get around with my understanding of CGContextclip. I am doing a simple program with custom UIButton drawrect as below
Rect dimension passed to drawRect method is CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef colorRef = [[UIColor blueColor] CGColor];
CGContextSetStrokeColorWithColor(context, colorRef);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextSetLineWidth(context, 3.0f);
CGContextAddRect(context, rect);
CGContextStrokeRect(context, rect);
//CGContextFillRect(context, rect);
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetFillColorWithColor(context, [[UIColor redColor]CGColor]);
CGMutablePathRef rectPath = CGPathCreateMutable();
CGPathMoveToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMidY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGPathAddLineToPoint(rectPath, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGPathCloseSubpath(rectPath);
CGContextAddPath(context, rectPath);
CGContextClip(context);
CGContextStrokePath(context);
//CGContextFillPath(context);
CGContextRestoreGState(context);
CGColorRelease(colorRef);
}
#end
I am expecting a full rect with dimension (0,0,100,50) colored in blue and a half rect inside with red coloured. I am getting the blue colored rect but red colored rect is not visible in my context.
I have gone through some solution here and also at some blogs but to my faulty eyes the code looks simple and fine. What is wrong with my clipping.
Thank you for your time and response.
I think there's no path in the context anymore after you clipped it.Just fill another rectangle:
//CGContextFillPath(context);
CGContextFillRect(context, rect);
My goal is
1. add gradient for my view (done)
2. add drop shadow for the bottom edge of my view ( issue at here )
What I am doing is :
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
UIColor *lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
CGRect paperRect = self.bounds;
// Fill with gradient
[self drawLinearGradient:context for:paperRect start:whiteColor.CGColor end:lightGrayColor.CGColor];
CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextSetLineWidth(context, .9);
CGContextStrokeRect(context, paperRect);
// Add shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, self.frame.size.height), 9.0, [UIColor blueColor].CGColor);
}
-(void)drawLinearGradient:(CGContextRef)context for:(CGRect)rect start:(CGColorRef)startColor end:(CGColorRef)endColor {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
However, what I am getting is the picture below without shadow at all
Am I on the right track but missing something in the middle ? Please help if you have any ideas..
You need to set the shadow properties before drawing whatever it is that's supposed to have the shadow. Put your call to CGContextSetShadowWithColor before the call to CGContextStrokeRect.
Also, the offset should probably not be as large as you're making it. The shadow offset controls how much the shadow is offset from the pixels that were drawn with that shadow, so unless you want the shadow to actually start very far away from your rect you probably want an offset of just a pixel or so, like CGSizeMake(0.0, 1.0).