I have succeeded in drawing a rectangle which has a hole inside of the shape of a rounded rectangle using CGContextEOFillPath. I can change the colour of my shape. However, I would like to use a gradient, I suppose that I have got to use CGContextDrawLinearGradient, however, I can't make it work. It keep painting the entire rectangle, hole included.
Here is the code that I am using
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGFloat frameWidth = 1; //[CQMFloatingContentOverlayView frameWidth];
CGFloat radius = [self cornerRadius];
CGSize viewSize = [self frame].size;
CGPathRef path = CQMPathCreateRoundingRect(CGRectMake(frameWidth, frameWidth,
viewSize.width - frameWidth * 2,
viewSize.height - frameWidth * 2),
radius, radius, radius, radius);
CGContextAddRect(context, CGRectMake(0, 0, viewSize.width, viewSize.height));
CGContextAddPath(context, path);
CGContextEOFillPath(context);
CGPathRelease(path);
CGContextRestoreGState(context);
}
CGPathRef CQMPathCreateRoundingRect(CGRect rect, CGFloat blRadius, CGFloat brRadius, CGFloat trRadius, CGFloat tlRadius) {
CGPoint tlPoint = rect.origin;
CGPoint brPoint = CGPointMake(rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, tlPoint.x + tlRadius, tlPoint.y);
CGPathAddArcToPoint(path, NULL,
brPoint.x, tlPoint.y,
brPoint.x, tlPoint.y + trRadius,
trRadius);
CGPathAddArcToPoint(path, NULL,
brPoint.x, brPoint.y,
brPoint.x - brRadius, brPoint.y,
brRadius);
CGPathAddArcToPoint(path, NULL,
tlPoint.x, brPoint.y,
tlPoint.x, brPoint.y - blRadius,
blRadius);
CGPathAddArcToPoint(path, NULL,
tlPoint.x, tlPoint.y,
tlPoint.x + tlRadius, tlPoint.y,
tlRadius);
CGPathCloseSubpath(path);
return path;
}
I don't know where to put the gradient code, I have tried before and after the CGContextEOFillPath but could not reach my objective.
This code should make what you want (check for syntax and typos, code is directly written in answer)
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat gradientColors[] =
{
0.1, 0.1, 0.5, 1.00,
0.9, 0.9, 1.0, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, NULL, sizeof(gradientColors)/(sizeof(gradientColors[0])*4));
CGColorSpaceRelease(colorSpace);
CGContextSaveGState(context);
CGFloat frameWidth = 1; //[CQMFloatingContentOverlayView frameWidth];
CGFloat radius = [self cornerRadius];
CGSize viewSize = [self frame].size;
CGPathRef path = CQMPathCreateRoundingRect(CGRectMake(frameWidth, frameWidth,
viewSize.width - frameWidth * 2,
viewSize.height - frameWidth * 2),
radius, radius, radius, radius);
CGContextAddRect(context, CGRectMake(0, 0, viewSize.width, viewSize.height));
CGContextAddPath(context, path);
CGContextEOClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(viewSize.width, viewSize.height), kCGGradientDrawsBeforeStartLocation);
CGPathRelease(path);
CGContextRestoreGState(context);
}
Related
this pieces of code won't work, i don't know why.
just create a trangle and clip then draw gradient
// create contect and save
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// create path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 20, rect.size.height - 20);
CGPathAddLineToPoint(path, NULL, rect.size.width / 2, 20);
CGPathAddLineToPoint(path, NULL, rect.size.width - 20, rect.size.height - 20);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
CGContextClip(context);
// create gradient
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray *colorArr = #[
(id)[UIColor blackColor],
(id)[UIColor whiteColor],
];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colorArr, NULL);
// rlease gradinet
CGContextDrawLinearGradient(context, gradient, CGPointMake(rect.size.width / 2,rect.size.height - 20), CGPointMake(rect.size.width / 2, 20), 0);
// restore context
CGContextRestoreGState(context);
After upload this question, i find my error, the color should be CGColor,
like this [UIColor redColor].cgColor.
I have been trying to measure glyph bounds precisely but this code prints out 916!!! The real width of this is 69.
- (void)drawRect:(NSRect)dirtyRect {
CGContextRef main = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetTextMatrix(main, CGAffineTransformIdentity);
CGGlyph g;
CGPoint p = CGPointMake(100, 100);
CGRect rect = CGRectMake(0, 0, 0, 0);
CGFontRef font = CGFontCreateWithFontName((CFStringRef)#"Arial");
g = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
CGContextSetFont(main, font);
CGContextSetTextPosition(main, 0, 0);
CGContextSetFontSize(main, 200);
CGContextSetRGBFillColor(main, 0, 0, 1, 1);
CGContextShowGlyphsAtPositions(main, &g, &p, 1);
CGFontGetGlyphBBoxes(font, &g, 1, &rect);
printf("%f", rect.size.width);
}
You are using CGFontGetGlyphBBoxes, which returns the size in glyph space units. To use this, you need to scale it with the units per em and the font size.
CGRect rect;
CGFontRef font = CGFontCreateWithFontName((CFStringRef)#"Arial");
CGFloat fontSize = 200.0;
CGGlyph g = CGFontGetGlyphWithGlyphName(font, CFSTR("L"));
CGFontGetGlyphBBoxes(font, &g, 1, &rect);
CGFloat width = rect.size.Width / CGFontGetUnitsPerEm(font) * fontSize;
An alternate way to do it to use [NSFont boundingRectForCGGlyph:].
NSFont *font = [NSFont fontWithName:#"Arial" size:200];
NSRect rect = [font boundingRectForCGGlyph:g];
boundingRectForCGGlyph
Returns the bounding rectangle for the specified glyph, scaled to the receiver’s size.
I was following a Ray Wenderlich tutorial on creating arcs:
http://www.raywenderlich.com/33193/core-graphics-tutorial-arcs-and-paths
-what I am looking to do is begin at point a (a fixed point) and locate where the user touches the screen, if it is + point A, then I want to draw an arc to that point. Though I don't return any errors I also don't get the path to be stroked. Could someone review the code below and see what I am doing wrong?
#implementation KIP_Arc
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) setArc {
//set the frame
float frameX = _startPoint.x;
float frameY = _startPoint.y;
float frameW = _endPoint.x;
float frameH = 50.0;
[self setFrame:CGRectMake(frameX, frameY, frameW, frameH)];
self.backgroundColor = [UIColor clearColor];
}
- (BOOL)isFlipped {
return YES;
}
- (void)drawRect:(CGRect)rect {
[[UIColor blackColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGRect arcRect = self.frame;
CGMutablePathRef arcPath = [self createArcPathFromBottomOfRect:arcRect:25.0];
CGContextAddPath(context, arcPath);
CGContextClip(context);
CGContextFillPath(context);
CGContextRestoreGState(context);
CFRelease(arcPath);
}
- (CGMutablePathRef) createArcPathFromBottomOfRect : (CGRect) rect : (CGFloat) arcHeight {
CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height - arcHeight, rect.size.width, arcHeight);
CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8*arcRect.size.height));
CGPoint arcCenter = CGPointMake(arcRect.origin.x + arcRect.size.width/2, arcRect.origin.y + arcRadius);
CGFloat angle = acos(arcRect.size.width / (2*arcRadius));
CGFloat startAngle = radians(180) + angle;
CGFloat endAngle = radians(360) - angle;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, endAngle, 0);
CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
return path;
}
static inline double radians (double degrees){
return degrees * M_PI/180;
}
CGContextClip, as a side effect, empties the current path. When you call CGContextFillPath immediately afterward, the current path is empty, so you fill nothing.
As its name implies, CGContextFillPath restricts itself to the context's current path. (If it didn't, its name would just be CGContextFill.) So, you don't need to clip.
Drop the CGContextClip call and just use CGContextFillPath.
I am Rotating the image by using this code
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle
{
CGFloat angleInRadians = angle * (M_PI / 360);
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect imgRect = CGRectMake(0, 0, width, height);
CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL,
rotatedRect.size.width,
rotatedRect.size.height,
8,
0,
colorSpace,
kCGImageAlphaPremultipliedFirst);
CGContextSetAllowsAntialiasing(bmContext, YES);
CGContextSetShouldAntialias(bmContext, YES);
CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
CGColorSpaceRelease(colorSpace);
CGContextTranslateCTM(bmContext,
+(rotatedRect.size.width/2),
+(rotatedRect.size.height/2));
CGContextRotateCTM(bmContext, angleInRadians);
CGContextTranslateCTM(bmContext,
-(rotatedRect.size.width/2),
-(rotatedRect.size.height/2));
CGContextDrawImage(bmContext, CGRectMake(0, 0,
rotatedRect.size.width,
rotatedRect.size.height),
imgRef);
CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
CFRelease(bmContext);
// [(id)rotatedImage autorelease];
return rotatedImage;
}
By this i am having the resultant image like this..
those white spaces should not be there and it should follow the same pattern of that size with out that white spaces around..the rotated image....
Please help..Thanks in advance
Instruments shows me that I have a leak in the following code:
CGContextMoveToPoint(c, startPoint.x, self.frame.size.height - offsetYBottom);
CGContextAddLineToPoint(c, startPoint.x, startPoint.y);
CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
CGContextAddLineToPoint(c, endPoint.x, self.frame.size.height - offsetYBottom);
CGContextClosePath(c);
CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 0.0/255.0, 197.0/255.0, 254.0/255.0, 1.0f, 0.0/255.0, 197.0/255.0, 254.0/255.0, 0.25f };
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);
CGPoint myStartPoint, myEndPoint;
myStartPoint.x = self.frame.size.width / 2;
myStartPoint.y = 0.0;
myEndPoint.x = self.frame.size.width / 2;
myEndPoint.y = self.frame.size.height;
CGContextSaveGState(c);
CGContextClip(c);
CGContextDrawLinearGradient (c, myGradient, myStartPoint, myEndPoint, 0);
CGContextRestoreGState(c);
If I comment this portion, the leaks are gone.
startPoint and endPoint are CGPoint.
Responsible caller: CGTypeCreateInstanceWithAllocator.
What could be the problem?
Following the Create Rule, you have to release myColorspace and myGradient when you're done with them:
CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);
Try to release myGradient object
CGGradientRelease(myGradient);