如何清除iOS中CGContext中的圆圈 [英] How to clear circle in CGContext in iOS

查看:352
本文介绍了如何清除iOS中CGContext中的圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CGcontext创建图像.这是有白色或黑色背景的简单的图象.我也想添加圆形的透明部分(检查附件图像).我知道该怎么做.但是我想让它转圈.请任何人在这方面帮助我.

I want create image using CGcontext. This is simple image with white or black background. also I want to add transperent part which is in circle ( check attached image). I know how to do this in rect. But i want to make it circle. Please anyone help me in this.

推荐答案

使用以下代码清除上下文中的圆圈

Use the below code to clear circle in your context

-(UIImage *) getImageWithcenterClear:(CGPoint) center{

    CGRect frame = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size,
                                       NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5 ] CGColor]);
    CGContextFillRect(context, frame);

    float radius = 50 * 2;
    // Clear Circle

    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextAddArc(context, center.x, center.y, radius - 0.54, 0, 2 * M_PI, 0);
    CGContextDrawPath(context, kCGPathFill);
    CGContextSetBlendMode(context, kCGBlendModeNormal);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

这篇关于如何清除iOS中CGContext中的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆