iOS imitates a D's avatar to draw a circle

Source: Internet
Author: User

Learn an app today, use a name to make a picture, draw a circle, write inside

At first, the intention to do the avatar is image format. So just:

cgrect rect = CGRectMake (0.0f, 0.0f, size. width, size. height);

Uigraphicsbeginimagecontext (rect.size);

Cgcontextref context = Uigraphicsgetcurrentcontext();

Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);

Cgcontextfillellipseinrect(context, rect);

// write

Cgcontextsetlinewidth(Context, 1.0);

Cgcontextsetrgbfillcolor (context, 0, 0, 0, 1);

Uifont *font = [Uifont boldsystemfontofsize:12.0];

[Sting Drawinrect:CGRectMake (3.5, 7.5, + ) Withfont:font];

UIImage *image = Uigraphicsgetimagefromcurrentimagecontext();

Uigraphicsendimagecontext();

return image;

But the returned circle is not smooth and jagged. The egg hurts.

Then, rewrite the view's DrawRect method

-(ID) initWithFrame: (CGRect) frame {

Self = [super Initwithframe:frame];

if (self) {

[self setbackgroundcolor: [Uicolor Clearcolor];

}

return self;

}

-(void) DrawRect: (cgrect) rect

{

//An opaque type of Quartz 2D painting Environment , equivalent to a canvas , you can paint on the above

Cgcontextref context = Uigraphicsgetcurrentcontext();

// draw a circle

Cgcontextsetfillcolorwithcolor(context, self. Imgcolor. Cgcolor);

Cgcontextfillellipseinrect(context, rect);

// write

Cgcontextsetlinewidth(Context, 1.0);

Cgcontextsetrgbfillcolor (context, 0, 0, 0, 1);

Uifont *font = [Uifont systemfontofsize:12.0];

[self. Title drawinrect: CGRectMake(3.5, 8, + ) Withfont: Font];

}

This is the solution to the problem, but OCD I ... The avatar is not supposed to be a picture. And, even if it's not. The problem above is not solved. That's not going to work.

Then a master prompts me to "understand that the earth is drawn to the image, set to the retain resolution, and the context that is captured in view itself is retain",

So, the first method of drawing the method, just need to put the green place, replaced by the following line of code

Uigraphicsbeginimagecontextwithoptions(size, NO, [uiscreen mainscreen].scale);

The scale parameter of this method is best set to [UIScreen mainscreen].scale, for what? Consistent with the system, hemp.

Well, I hope it will be useful to everyone, anyway I can reuse it for myself.

iOS imitates a D's avatar to draw a circle

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.