ios 仿某d的頭像畫圓

來源:互聯網
上載者:User

標籤:

今天學習某app,用名字做頭像,畫圓,裡面寫字

一開始,打算做頭像是image格式。所以就:

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

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillEllipseInRect(context, rect);

     //寫字

    CGContextSetLineWidth(context, 1.0);

    CGContextSetRGBFillColor (context, 0, 0, 0, 1);

    UIFont  *font = [UIFont boldSystemFontOfSize:12.0];

    [sting drawInRect:CGRectMake(3.5, 7.5, 25, 15) withFont:font];

 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

但是返回的圈圈不平滑,有鋸齒。蛋疼。。

然後,就重寫view的drawrect方法

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame];

    if (self) {

        [self setBackgroundColor:[UIColor clearColor]];

    }

    return self;

}

- (void)drawRect:(CGRect)rect

{

    //一個不透明類型的Quartz 2D繪畫環境,相當於一個畫布,你可以在上面任意繪畫

    CGContextRef context = UIGraphicsGetCurrentContext();

    //畫圓

    CGContextSetFillColorWithColor(context, self.imgColor.CGColor);

    CGContextFillEllipseInRect(context, rect);

    //寫字

    CGContextSetLineWidth(context, 1.0);

    CGContextSetRGBFillColor (context, 0, 0, 0, 1);

    UIFont  *font = [UIFont systemFontOfSize:12.0];

    [self.title drawInRect:CGRectMake(3.5, 8, 25, 15) withFont:font];

}

這個是解決了問題,但是強迫症的我。。。頭像不是應該圖片嗎。。而且,就算不是。。上面那個問題也沒有解決。這可不行。。

然後有高手提示我“理解是繪製到image上的土,設定為retain解析度,而view中擷取的context本身就是retain”,

於是,第一個方法中畫圖的方法,只需要把綠色的地方,換成下面這行代碼

UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

這個方法scale參數最好是設定成[UIScreen mainScreen].scale,為什麼呢。。跟系統一致麻。。

好了,希望對大家有用,反正我也是為了自己以後能複用。。

ios 仿某d的頭像畫圓

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.