iOS draws text on a picture, very simple

Source: Internet
Author: User

2D image rendering with Uigraphics do not use uigraphicsbeginimagecontext (size); Otherwise the picture will blur.

All operations of the Core Graphics API are performed in one context. So you need to get the context before drawing and pass in the function that performs the rendering. If you are rendering a picture in memory, you need to pass in the context in which the picture belongs. Getting a graphical context is the first step in completing the drawing task, and you can understand the graphical context as a canvas. If you don't get the canvas, you won't be able to complete any drawing operations. Of course, there are many ways to get a graphical context, and here I present two of the most commonly used methods of obtaining it. Call the Uigraphicsbeginimagecontextwithoptions function to get a graphical context for working with the picture. With this context, you can draw on it and generate pictures. Call the Uigraphicsgetimagefromcurrentimagecontext function to get a UIImage object from the current context. Remember to call the Uigraphicsendimagecontext function to close the graphics context after all your drawing operations. On the code:

-(UIImage *) Createshareimage: (nsstring *) str name: (nsstring *) name number: (nsstring *) Number grade: (nsstring *) grade

{

UIImage *image = [UIImage imagenamed:@ "Sharegrade"];

cgsize size=cgsizemake(image. Size. Width, image. Size. Height); // canvas size

uigraphicsbeginimagecontextwithoptions(size, NO, 0.0);

[Image Drawatpoint:cgpointmake(0, 0)];

// Get a bitmap graphics context

cgcontextref context=uigraphicsgetcurrentcontext();

Cgcontextdrawpath(context, kcgpathstroke);

// draw How many users have been defeated

[StrDrawatpoint:cgpointmake (30 image.< Span class= "S1" >size. Height*0.65) withattributes :@{nsfontattributename:[ Uifont fontwithname:@ "ARIAL-BOLDMT" size:30), nsforegroundcolorattributename:[uicolor Span class= "S1" >whitecolor]}];

//Draw the content you want to draw .....

// Returns a new drawing

UIImage *newimage=uigraphicsgetimagefromcurrentimagecontext();

Uigraphicsendimagecontext();

return newimage;

}

iOS draws text on a picture, very simple

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.