Quartz usage of Various Drawing Images-Implementation of image painting, text writing, line drawing, elliptical, rectangular, prism, etc.

Source: Internet
Author: User

// Only override drawrect: If you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(Void) drawrect :( cgrect) rect
{
Cgcontextref context = uigraphicsgetcurrentcontext ();

/* Draw a line at No.1
Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5); // line color
Cgcontextmovetopoint (context, 20, 20 );
Cgcontextaddlinetopoint (context, 20 );
Cgcontextstrokepath (context );
*/
 
/* No. 2 write text
Cgcontextsetlinewidth (context, 1.0 );
Cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 0.5 );
Uifont * font = [uifont boldsystemfontofsize: 18.0];
[@ "Company: Beijing zhongsoft Technology Co., Ltd. \ n Department: ERP division \ n name: mcliang" drawinrect: cgrectmake (20, 40,280,300) withfont: font];
*/
 
/* No. 3 draw a square image without Borders
Cgcontextsetrgbfillcolor (context, 0, 0.25, 0, 0.5 );
Cgcontextfillrect (context, cgrectmake (2, 2,270,270 ));
Cgcontextstrokepath (context );
*/

/* Draw a square border in No. 4
Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5); // line color
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextaddrect (context, cgrectmake (2, 2,270,270 ));
Cgcontextstrokepath (context );
*/
 
/* No. 5 draw a square background color
Cgcontexttranslatectm (context, 0.0f, self. bounds. Size. Height );
Cgcontextscalectm (context, 1.0f,-1.0f );
Uigraphicspushcontext (context );
Cgcontextsetlinewidth (context, 320 );
Cgcontextsetrgbstrokecolor (context, 250.0/255,250.0/255,210.0/255, 1.0 );
Cgcontextstrokerect (context, cgrecctmake (0, 0,320,460 ));
Uigraphicspopcontext ();
*/
 
/* No. 6 elliptic
Cgrect arect = cgrectmake (80, 80,160,100 );
Cgcontextsetrgbstrokecolor (context, 0.6, 0.9, 0, 1.0 );
Cgcontextsetlinewidth (context, 3.0 );
Cgcontextaddellipseinrect (context, arect); // elliptic
Cgcontextdrawpath (context, kcgpathstroke );
*/
 
/* No. 7
Cgcontextbeginpath (context );
Cgcontextsetrgbstrokecolor (context, 0, 0, 1, 1 );
Cgcontextmovetopoint (context, 100,100 );
Cgcontextaddarctopoint (context, 50,100, 50,150, 50 );
Cgcontextstrokepath (context );
*/
 
/* No. 8 gradient
Cgcontextclip (context );
Cgcolorspaceref RGB = cgcolorspacecreatedevicergb ();
Cgfloat colors [] =
{
204.0/255.0, 224.0/255.0, 244.0/255.0, 1.00,
29.0/255.0, 156.0/255.0, 215.0/255.0, 1.00,
0.0/255.0, 50.0/255.0, 126.0/255.0, 1.00,
};
Cggradientref gradient = cggradientcreatewithcolorcomponents
(RGB, colors, null, sizeof (colors)/(sizeof (colors [0]) * 4 ));
Cgcolorspacerelease (RGB );
Cgcontextdrawlineargradient (context, gradient, cgpointmake
(0.0, 0.0), cgpointmake (0.0, self. Frame. Size. Height ),
Kcggradientdrawsbeforestartlocation );
*/

/* Draw a square with four lines
// Draw a line
Uicolor * acolor = [uicolor colorwithred: 0 Green: 1.0 Blue: 0 ALPHA: 0];
Cgcontextsetrgbstrokecolor (context, 1.0, 0, 0, 1.0 );
Cgcontextsetfillcolorwithcolor (context, acolor. cgcolor );
Cgcontextsetlinewidth (context, 4.0 );
Cgpoint apoints [5];
Apoints [0] = cgpointmake (60, 60 );
Apoints [1] = cgpointmake (260, 60 );
Apoints [2] = cgpointmake (260,300 );
Apoints [3] = cgpointmake (60,300 );
Apoints [4] = cgpointmake (60, 60 );
Cgcontextaddlines (context, apoints, 5 );
Cgcontextdrawpath (context, kcgpathstroke); // start to draw a line
*/

/* No. 10
Uicolor * acolor = [uicolor colorwithred: 0 Green: 1.0 Blue: 0 ALPHA: 0];
Cgcontextsetrgbstrokecolor (context, 1.0, 0, 0, 1.0 );
Cgcontextsetfillcolorwithcolor (context, acolor. cgcolor );
// Elliptic
Cgrect arect = cgrectmake (80, 80,160,100 );
Cgcontextsetrgbstrokecolor (context, 0.6, 0.9, 0, 1.0 );
Cgcontextsetlinewidth (context, 3.0 );
Cgcontextsetfillcolorwithcolor (context, acolor. cgcolor );
Cgcontextaddrect (context, rect); // rectangle
Cgcontextaddellipseinrect (context, arect); // elliptic
Cgcontextdrawpath (context, kcgpathstroke );
*/

/* No. 11
Draw a solid circle
Cgcontextfillellipseinrect (context, cgrectmake (95, 95,100.0, 100 ));
*/

/* No. 12
Draw a diamond
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgcontextmovetopoint (context, 100,100 );
Cgcontextaddlinetopoint (context, 150,150 );
Cgcontextaddlinetopoint (context, 100,200 );
Cgcontextaddlinetopoint (context, 50,150 );
Cgcontextaddlinetopoint (context, 100,100 );
Cgcontextstrokepath (context );
*/
 
/* Draw a rectangle at No. 13
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgrect rectangle = cgrectmake (60,170,200, 80 );
Cgcontextaddrect (context, rectangle );
Cgcontextstrokepath (context );
*/

/* Elliptic
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgrect rectangle = cgrectmake (60,170,200, 80 );
Cgcontextaddellipseinrect (context, rectangle );
Cgcontextstrokepath (context );
*/

/* Fill a path in Red:
Cgcontextmovetopoint (context, 100,100 );
Cgcontextaddlinetopoint (context, 150,150 );
Cgcontextaddlinetopoint (context, 100,200 );
Cgcontextaddlinetopoint (context, 50,150 );
Cgcontextaddlinetopoint (context, 100,100 );
Cgcontextsetfillcolorwithcolor (context, [uicolor redcolor]. cgcolor );
Cgcontextfillpath (context );
*/

/* Fill in a red rectangle with a blue edge
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgrect rectangle = cgrectmake (60,170,200, 80 );
Cgcontextaddrect (context, rectangle );
Cgcontextstrokepath (context );
Cgcontextsetfillcolorwithcolor (context, [uicolor redcolor]. cgcolor );
Cgcontextfillrect (context, rectangle );
*/

/* Draw an arc
// The arc is drawn by specifying two cut points and angles and calling cgcontextaddarctopoint ().
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgcontextmovetopoint (context, 100,100 );
Cgcontextaddarctopoint (context, 100,200,300,200,100 );
Cgcontextstrokepath (context );
*/

/*
Plot the Beitz Curve
// The Beitz curve is drawn by moving a starting point and then using two control points and a stop point that calls the cgcontextaddcurvetopoint () function.
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgcontextmovetopoint (context, 10, 10 );
Cgcontextaddcurvetopoint (context, 0, 50,300,250,300,400 );

Cgcontextstrokepath (context );
*/

/* Draw a quadratic Beitz Curve
Cgcontextsetlinewidth (context, 2.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgcontextmovetopoint (context, 10,200 );
Cgcontextaddquadcurvetopoint (context, 150, 10,300,200 );
Cgcontextstrokepath (context );
*/

/* Draw the dotted line
Cgcontextsetlinewidth (context, 5.0 );
Cgcontextsetstrokecolorwithcolor (context, [uicolor bluecolor]. cgcolor );
Cgfloat dasharray [] = {2, 6, 4 };
Cgcontextsetlinedash (context, 3, dasharray, 4); // skip 3 and draw the dotted line, so there are 6-(3-2) = 5 virtual points at the beginning
Cgcontextmovetopoint (context, 10,200 );
Cgcontextaddquadcurvetopoint (context, 150, 10,300,200 );
Cgcontextstrokepath (context );
*/


/* Draw an image
Nsstring * ImagePath = [[nsbundle mainbundle] pathforresource: @ "dog" oftype: @ "PNG"];
Uiimage * myimageobj = [[uiimage alloc] initwithcontentsoffile: ImagePath];
// [Myimageobj drawatpoint: cgpointmake (0, 0)];
[Myimageobj drawinrect: cgrectmake (0, 0,320,480)];
Nsstring * s = @ "my puppies ";
[S drawatpoint: cgpointmake (100, 0) withfont: [uifont systemfontofsize: 34.0];
*/

/*
Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "dog" oftype: @ "PNG"];
Uiimage * IMG = [uiimage imagewithcontentsoffile: path];
Cgimageref image = IMG. cgimage;
Cgcontextsavegstate (context );
Cgrect touchrect = cgrectmake (0, 0, IMG. Size. Width, IMG. Size. Height );
Cgcontextdrawimage (context, touchrect, image );
Cgcontextrestoregstate (context );
*/

/* Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "dog" oftype: @ "PNG"];
Uiimage * IMG = [uiimage imagewithcontentsoffile: path];
Cgimageref image = IMG. cgimage;
Cgcontextsavegstate (context );
 
Cgcontextrotatectm (context, m_pi );
Cgcontexttranslatectm (context,-IMG. Size. Width,-IMG. Size. Height );
 
Cgrect touchrect = cgrectmake (0, 0, IMG. Size. Width, IMG. Size. Height );
Cgcontextdrawimage (context, touchrect, image );
Cgcontextrestoregstate (context );*/
 
/*
Nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "dog" oftype: @ "PNG"];
Uiimage * IMG = [uiimage imagewithcontentsoffile: path];
Cgimageref image = IMG. cgimage;

Cgcontextsavegstate (context );
 
Cgaffinetransform myaffine = cgaffinetransformmakerotation (m_pi );
Myaffine = cgaffinetransformtranslate (myaffine,-IMG. Size. Width,-IMG. Size. Height );
Cgcontextconcatctm (context, myaffine );
 
Cgcontextrotatectm (context, m_pi );
Cgcontexttranslatectm (context,-IMG. Size. Width,-IMG. Size. Height );
 
Cgrect touchrect = cgrectmake (0, 0, IMG. Size. Width, IMG. Size. Height );
Cgcontextdrawimage (context, touchrect, image );
Cgcontextrestoregstate (context );
*/
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.