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