CoreGraphicsで円のUIImageを作る

はじめに

ゆう@あんのうんです。

今日、仕事中にど忘れしていたので、今後の自分への備忘録も込めて

外周を別な色で囲った円を書きます。

実際のコード

#define LC_TINTCOLOR_RED    0.992
#define LC_TINTCOLOR_BLUE   0.565
#define LC_TINTCOLOR_GREEN  0.149
#define LC_TINTCOLOR_ALPHA  1.0

- (UIImage *)convertImage {
    UIImage *resultImage = nil;
    UIGraphicsBeginImageContext(_ibHeaderImageView.frame.size);
    float mergineR = 1.0f;
    CGRect circleRect = CGRectMake(mergineR, mergineR, _ibHeaderImageView.frame.size.width-mergineR*2, _ibHeaderImageView.frame.size.height-mergineR*2);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetRGBStrokeColor(context, 1, 1, 0, 1);
    CGContextSetLineWidth(context, 2.0);
    CGContextStrokeEllipseInRect(context, circleRect);
    
    CGContextSetRGBFillColor(context, LC_TINTCOLOR_RED, LC_TINTCOLOR_GREEN, LC_TINTCOLOR_BLUE, LC_TINTCOLOR_ALPHA);
    CGContextFillEllipseInRect(context, circleRect);
    
    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImage;
}

結果は以下な感じ

f:id:project-unknown:20150107192506p:plain