ios-quartz2d Drawing

Source: Internet
Author: User

Often said warm so know new, after so long again to see these basic content is still very excited! Share the code of the knocking, hoping to provide a slightest inspiration to the reader

/** * Crop round picture * *-(void) circleicon{UIImage *image = [UIImage imagenamed:@"Baby"];    Uigraphicsbeginimagecontext (image.size); Uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, Image.size.width, Image.size.height)];    [Path Addclip];    [Image Drawatpoint:cgpointzero];    UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); Self.imageV.image = NewImage;}/** * Image plus watermark * *-(void) watermark{UIImage *image= [UIImage imagenamed:@"Baby"];//Open Picture ContextUigraphicsbeginimagecontextwithoptions (Image.size, NO,0.0);//Draw picture to picture context[Image Drawatpoint:cgpointzero];//Draw textNSString *str = @"Chu Cowei"; [Str drawatpoint:cgpointzero withattributes:@{nsfontattributename:[uifont systemfontofsize: -]}];//Generate picturesUIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();//Close the context manuallyUigraphicsendimagecontext (); Self.imageV.image = NewImage;} -(void) awakefromnib{//[self timer];}/** * Graphics Context Matrix Operation */-(void) drawmatrix{//1. Opening a contextCgcontextref CTX = Uigraphicsgetcurrentcontext ();//2. Drawing PathsUibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake ( -, -, $, -)]; [[Uicolor Redcolor] set];//Context matrix operations    //Panning//CGCONTEXTTRANSLATECTM (CTX, -50, -50);    //ZoomCGCONTEXTSCALECTM (CTX,0.5,0.5);//RotateCGCONTEXTROTATECTM (CTX, m_pi_4);//3. Adding a path to a graphics contextCgcontextaddpath (CTX, path.) Cgpath);//4. Render the sewageCgcontextfillpath (CTX);}/** * Timer Snow * *-(void) timer{//Timer Method 1-Stalling//Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:0.02 target:self selector: @selector (updatesnow) userIn Fo:nil Repeats:yes];    //Timer Method 2-Called when the screen is refreshed every time (no seconds are refreshed 60 times)Cadisplaylink *link = [Cadisplaylink displaylinkwithtarget:self selector:@selector(Updatesnow)]; [link Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];} -(void) updatesnow{_y+=Ten;if(_y > Self.bounds.size.height) {_y =0; } [self setneedsdisplay];}Static int_y =0;/** * Painting pictures * *-(void) Drawimagewith: (cgrect) rect{UIImage *image = [UIImage imagenamed:@"Snow"];//Keep picture source size[Image Drawatpoint:cgpointmake (Ten, _y)];//Fill the entire view//[Image drawInRect:self.bounds];    //Fill the image with the entire view//[Image drawAsPatternInRect:self.bounds];}/** * Painting text * *-(void) Drawtextwith: (cgrect) rect{nsstring *str = @"Chu Cowei";    Nsmutabledictionary *attri = [Nsmutabledictionary dictionary]; [Attri Setobject:[uifont Systemfontofsize: -] Forkey:nsfontattributename]; [Attri Setobject:[uicolor Whitecolor] forkey:nsforegroundcolorattributename];//does not wrap automatically[str Drawatpoint:cgpointzero Withattributes:attri];//will be wrapped automatically[str drawinrect:rect withattributes:attri];}/** * appeased map * /-(void) Drawprewithrect: (cgrect) rect{nsarray *anglearr = @[@ -,@ -,@ -]; Cgpoint Center = cgpointmake (rect.size.width*0.5, rect.size.height*0.5); CGFloat radius = rect.size.width*0.5-Ten; CGFloat Angle =0; CGFloat startangle =0; CGFloat Endangle =0; for(NSNumber *num in Anglearr)        {startangle = Endangle; Angle = num.intvalue/100.0*m_pi*2;        Endangle = StartAngle + Angle; Uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center radius:radius startangle:startangle endAngle:        Endangle Clockwise:yes];        [Path Addlinetopoint:center];        [[Self randomcolor] set];    [Path fill]; }}//Random color-(Uicolor *) randomcolor{cgfloat r = Arc4random_uniform ( the) /255.0; CGFloat g = Arc4random_uniform ( the) /255.0; CGFloat B = Arc4random_uniform ( the) /255.0;return[Uicolor colorwithred:r green:g blue:b Alpha:1];}/** * Draw progress bar * /-(void) Setprogressvalue: (cgfloat) progressvalue{if(_progressvalue! = progressvalue)        {_progressvalue = Progressvalue;    [Self setneedsdisplay]; }}-(void) Drawprogresswithrect: (cgrect) rect{Cgpoint Center = cgpointmake (rect.size.width*0.5, rect.size.height*0.5); CGFloat radius = rect.size.width*0.5-Ten;    CGFloat startangle =-m_pi_2; CGFloat endangle =-m_pi_2 + self.progressvalue*m_pi*2; Uibezierpath *progresspath = [Uibezierpath bezierpathwitharccenter:center radius:radius StartAngle:startAngle    Endangle:endangle Clockwise:yes]; [Progresspath stroke];}/** * Draw circles and arcs * *-(void) Drawcirclewithrect: (CGRect) rect{//1. Radius-Fillet radiusUibezierpath *circel0 = [Uibezierpath bezierpathwithroundedrect:cgrectmake ( -, -, -, -) Cornerradius:Ten];//[circel0 stroke];Uibezierpath *circle1 = [Uibezierpath bezierpathwithovalinrect:cgrectmake ( -, -, -, -)];//[circle1 fill];    //ARC drawing    //center: Center    //radius: Radius    //startangle: Start angle 0: Circle Right    //endangle: End Angle    //clockwise YES: Clockwise NO: CounterclockwiseCgpoint Center = cgpointmake (rect.size.width*0.5, rect.size.height*0.5); Uibezierpath *path = [Uibezierpath bezierpathwitharccenter:cgpointmake (rect.size.width*0.5, rect.size.height*0.5) Radius: -StartAngle:0Endangle:-m_pi_2 Clockwise:no]; [Path Addlinetopoint:center];//[Path Addlinetopoint:cgpointmake (center.x+50, Center.y)];    //Upstream code can use closed path[Path Closepath]; [Path stroke];}/** * Draw a rectangle * *-(void) drawrect{Cgcontextref contextref = Uigraphicsgetcurrentcontext (); Uibezierpath *path = [Uibezierpath bezierpathwithrect:cgrectmake (Ten,Ten, -, -)]; Cgcontextaddpath (contextref, path.    Cgpath); Cgcontextstrokepath (CONTEXTREF);}/** * Draw round * *-(void) drawquadcurve{Cgcontextref textref = Uigraphicsgetcurrentcontext ();    Uibezierpath *path = [Uibezierpath Bezierpath]; [Path Movetopoint:cgpointmake ( -, the)]; [Path Addquadcurvetopoint:cgpointmake ( $, the) Controlpoint:cgpointmake ( the,Ten)]; Cgcontextsetlinewidth (Textref,Ten); Cgcontextaddpath (textref, path.    Cgpath); Cgcontextstrokepath (TEXTREF);}/** * Draw line * *-(void) drawline{//1. Get a view-related contextCgcontextref Contextrex = Uigraphicsgetcurrentcontext ();//2.1 Description PathUibezierpath *bezierpath = [Uibezierpath Bezierpath];//2.2 Setting the starting point[Bezierpath Movetopoint:cgpointmake (Ten, -)];//2.3 Setting the end point[Bezierpath Addlinetopoint:cgpointmake ( -, -)];//2.4 set an end point again[Bezierpath Addlinetopoint:cgpointmake ( -, -)];//* Setting path Properties-Context State    //WidthCgcontextsetlinewidth (Contextrex,Ten);//Line connection style    //Kcglinejoinmiter, default    //Kcglinejoinround, Arc    //Kcglinejoinbevel squareCgcontextsetlinejoin (Contextrex, kcglinejoinbevel);//Color[[Uicolor Redcolor] setstroke];//3. Adding a path to the contextCgcontextaddpath (Contextrex, Bezierpath.cgpath);//4. Displaying the context on the viewCgcontextstrokepath (Contextrex);}

ios-quartz2d Drawing

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.