【问题标题】:How to clear circle in CGContext in iOS如何在iOS中清除CGContext中的圆圈
【发布时间】:2016-02-08 09:50:55
【问题描述】:

我想使用 CGcontext 创建图像。这是具有白色或黑色背景的简单图像。我还想添加圆形的透明部分(检查附图)。我知道如何正确地做到这一点。但我想让它圈起来。请任何人帮助我。

【问题讨论】:

标签: ios iphone image-processing cgcontextref cgcontextdrawimage


【解决方案1】:

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

-(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;

}

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多