CLOSE SEARCH
POSTS TAGGED WITH: Quartz

[Snippet] Draw Bezier Line using Quartz (태극마크)

– (void)drawBezierLine:(CGContextRef)context
{

CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);

CGContextAddEllipseInRect(context, CGRectMake(60, 20, 200, 200));
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextStrokePath(context);

CGPoint start = CGPointMake(60.0, 120.0);
CGPoint controlPoint1 = CGPointMake(130.0, 230.0);
CGPoint controlPoint2 = CGPointMake(190.0, 10.0);
CGPoint end = CGPointMake(260.0, 120.0);

CGContextMoveToPoint(context, start.x, start.y);
CGContextAddCurveToPoint(context, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, end.x, end.y);
CGContextSaveGState(context);

CGContextAddArc(context, 160, 120, 100, 0, M_PI, true);
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextStrokePath(context);

}

[Snippet] Draw Font using Quartz

– (void)drawFont:(CGContextRef)context
{

CGContextSelectFont(context, “Georgia”, 24.0, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1);

CGAffineTransform xForm = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xForm);
CGContextShowTextAtPoint(context, 10, 30, “Georgia”, strlen(“Georgia”));

}

[Snippet] Draw Gradient using Quartz

– (void)drawGradient:(CGContextRef)context
{

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
1.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0,
0.0, 0.0, 0.0, 1.0
};

CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(rgb);

CGRect rect = CGRectMake(20.0, 10.0, 50.0, 200.0);
CGContextClipToRect(context, rect);
CGContextDrawLinearGradient(context, gradient, CGPointMake(40, 10), CGPointMake(40, 210), 0);

CGContextSetLineWidth(context, 4.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, CGContextGetClipBoundingBox(context));

}

[Snippet] Draw PDF document using Quartz

– (void)drawPDF:(CGContextRef)context
{

CFURLRef pdf = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR(“test.pdf”), NULL, NULL);
CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL((CFURLRef)pdf);
CFRelease(pdf);

CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(doc, 1);

CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

}

[Snippet] Draw Image using Quartz

– (void)drawImage:(CGContextRef)context
{

NSString *imgFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@”img.png”];
CGDataProviderRef provider = CGDataProviderCreateWithFilename([imgFileName UTF8String]);
CGImageRef imgRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);

//or
//UIImage *img = [UIImage imageNamed:@”img.png”];
//CGImageRef imgRef = CGImageRetain(img.CGImage);

CGContextClipToRect(context, CGRectMake(10.0, 10.0, self.bounds.size.width – 20.0, self.bounds.size.height – 20.0));
CGContextDrawTiledImage(context, CGRectMake(0, 0, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)), imgRef);

CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRect(context, CGContextGetClipBoundingBox(context));

}

[Snippet] Draw Polygon using Quartz

– (void)drawPolygon:(CGContextRef)context
{

//star
CGPoint center = CGPointMake(80.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 5; ++i)
{

CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0);

CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);

CGContextAddLineToPoint(context, center.x + x, center.y + y);

}

CGContextClosePath(context);
CGContextFillPath(context);

//star + circle
center = CGPointMake(160.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 5; i++)
{

CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0);
CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);

}
CGContextAddEllipseInRect(context, CGRectMake(130, 70, 60, 60));
CGContextClosePath(context);
CGContextEOFillPath(context);

//pentagon
center = CGPointMake(240.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 6; i++)
{

CGFloat x = 30.0 * sinf(i * 2.0 * M_PI / 6.0);
CGFloat y = 30.0 * cosf(i * 2.0 * M_PI / 6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);

}
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

}

 

[Snippet] Draw Rectangle using Quartz

– (void)drawRectangle:(CGContextRef)context
{

CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextSetLineWidth(context, 2.0);
//
CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
CGContextStrokePath(context);
//
CGContextFillRect(context, CGRectMake(130.0, 30.0, 60.0, 60.0));
//
CGContextStrokeRectWithWidth(context, CGRectMake(230.0, 30.0, 60.0, 60.0), 4.0);
}

[Snippet] Draw Line using Quartz

– (void)drawLine:(CGContextRef)context

{
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 4.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(context, 10.0, 50.0);
CGContextAddLineToPoint(context, 310.0, 50.0);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGFloat dash[] = {5.0, 10.0, 15.0, 20.0};
CGContextSetLineDash(context, 0.0, dash, 4);
CGContextMoveToPoint(context, 10.0, 70.0);
CGContextAddLineToPoint(context, 310.0, 70.0);
CGContextStrokePath(context);
CGContextSetLineDash(context, 0.0, NULL, 0);
CGPoint lines[] =
{
CGPointMake(10.0, 90.0),
CGPointMake(60.0, 140.0),
CGPointMake(110.0, 130.0),
CGPointMake(160.0, 190.0),
CGPointMake(210.0, 200.0),
CGPointMake(260.0, 90.0),
CGPointMake(310.0, 200.0)
};
CGContextSetLineWidth(context, 2.0);
CGContextAddLines(context, lines, sizeof(lines) / sizeof(lines[0]));
CGContextStrokePath(context);
}