In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "iOS development CGContextRef drawing how to use", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "iOS development CGContextRef drawing how to use" article.
1. Create a canvas
CGContextRef ctx = UIGraphicsGetCurrentContext ()
two。 Set properti
/ / rotate, note: the setting operation must be done before adding the drawing, and if it is set after adding the drawing, it is already finished. When the rotation is invalid, the whole layer is rotated / / rotated 45 degrees CGContextRotateCTM (ctx, M_PI_4); / / scale: 0.5x in x direction and 1.5x CGContextScaleCTM in y direction (ctx, 0.5,1.5) / / translate: move in the x direction 50pm 100CGContextTranslateCTM (ctx, 50,100); CGContextSetLineJoin (ctx, kCGLineJoinRound); / / Line width CGContextSetLineWidth (ctx, 1.0); / / start and end fillet CGContextSetLineCap (ctx, kCGLineCapRound); / / Corner fillet CGContextSetLineJoin (ctx, kCGLineJoinRound); / / Transparency CGContextSetAlpha (ctx, 0.5)
3. Draw a straight line
/ / start CGContextMoveToPoint (ctx, 10.0,1.0); / / end CGContextAddLineToPoint (ctx, self.frame.size.width-20.0, 100.0); / / both colors can be set by / / CGContextSetRGBStrokeColor (ctx, 0,1.0,0,1.0); [[UIColor redColor] set]; / / rendering, lines can only be drawn hollow, and cannot call CGContextFillPath (ctx) / / or use this method: CGContextDrawPath (ctx, kCGPathStroke) CGContextStrokePath (ctx)
Or use the following method to draw a straight line
CGPoint point [2]; / coordinate point point [0] = CGPointMake (10.0, 100.0); / / start point point [1] = CGPointMake (self.frame.size.width-20.0, 100.0); / / end point / / points [] coordinate array, and count size CGContextAddLines (context, aPoints, 2); / / add line CGContextDrawPath (context, kCGPathStroke)
4. Draw a dotted line
/ / set the dashed line color CGContextSetStrokeColorWithColor (ctx, [UIColor redColor] .CGColor); / / set the dashed line drawing starting point CGContextMoveToPoint (ctx, 10.0,50.0); / / set the dashed line drawing end point CGContextAddLineToPoint (ctx, self.frame.size.width-20.0, 50.0); / / set the width interval of the dotted line arrangement: the number in the following arr indicates that three points are drawn first and then one point CGFloat arr [] = {3,2} / / the last parameter "2" below represents the number of permutations. CGContextSetLineDash (ctx, 0, arr, 2); CGContextDrawPath (ctx, kCGPathStroke)
5. Draw triangles
/ / start point CGContextMoveToPoint (ctx, self.center.x, 200.0); / / inflection point 1CGContextAddLineToPoint (ctx, self.center.x-50.0, 250.0); / / end point CGContextAddLineToPoint (ctx, self.center.x+50.0, 250.0); / / CGContextSetRGBStrokeColor (ctx, 0,1.0,0,1.0); [UIColor redColor] set]; / / merge triangle CGContextClosePath (ctx); CGContextFillPath (ctx)
6. Draw a rectangle
CGRect rectangle = CGRectMake (10.0, 300.0, self.frame.size.width-20.0, 60.0); CGContextAddRect (ctx, rectangle); CGContextSetFillColorWithColor (ctx, [UIColor redColor] .CGColor); CGContextFillPath (ctx)
7. Draw a circle
/ * * c current drawing x center coordinates xy center coordinates yradius radius startAngle arc start point and positive X axis angle between endAngle arc end point and positive X axis angle clockwise specify 0 to create a clockwise arc, or specify 1 to create a counterclockwise arc * / CGContextAddArc (ctx, self.center.x, 100.0, 75.0,0.0, M_PI+0.5, 0) CGContextSetFillColorWithColor (ctx, [UIColor redColor] .CGColor); CGContextFillPath (ctx)
8. Draw an ellipse
CGContextAddEllipseInRect (ctx, CGRectMake (x, y, 100.0); CGContextSetFillColorWithColor (ctx, [UIColor redColor] .CGColor); CGContextFillPath (ctx)
9. Draw a fan
CGContextMoveToPoint (ctx, x, y); CGContextAddArc (ctx, x, y, 75.0,0.0, M_PI+0.5, 0); CGContextSetFillColorWithColor (ctx, [UIColor redColor] .CGColor); CGContextSetStrokeColorWithColor (ctx, [UIColor blueColor] .CGColor); CGContextDrawPath (ctx, kCGPathFillStroke)
10. Draw a quadratic Bezier curve
CGContextMoveToPoint (context, 120,300); / / set the start point CGContextAddQuadCurveToPoint of the Path (context,190, 310,120,390); / / set the control point coordinates and end point coordinates of the Bezier curve CGContextStrokePath (context)
11. Draw a cubic Bezier curve
CGContextMoveToPoint (context, 200,300); / / set the starting point CGContextAddCurveToPoint of the Path (context,250, 280,250,400,280,300); / / set the control point coordinates of the Bezier curve and the control point coordinates end coordinates CGContextStrokePath (context)
twelve。 Draw words
/ / set text properties NSMutableDictionary * dict = [NSMutableDictionary dictionary]; dict [NSForegroundColorAttributeName] = [UIColor whiteColor]; dict [NSFontAttributeName] = [UIFont systemFontOfSize:14]; [@ "I Love iOS" drawInRect:rect withAttributes:dict]
13. Draw a picture
UIImage * image = [UIImage imageNamed:@ "apple.jpg"]; [image drawInRect:CGRectMake (60,340,20,20)]; / / draw a picture in coordinates / / [image drawAtPoint:CGPointMake (100,340)]; / / keep the size of the picture at the pointpoint and start drawing the picture. You can remove the comments and look at CGContextDrawImage (context, CGRectMake (100,340,20,20), image.CGImage). / / use this to make the picture upside down / / CGContextDrawTiledImage (context, CGRectMake (0,0,20,20), image.CGImage); / / the tiling above is about "how to use CGContextRef drawing in iOS development". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.