给UIImage添加外边框圆

来源:互联网 发布:身份证psd源码 编辑:程序博客网 时间:2024/05/19 21:59

代码如下:

- (UIImage *)addBorderToImage:(UIImage *)image {    CGImageRef bgimage = [image CGImage];    float width = CGImageGetWidth(bgimage);    float height = CGImageGetHeight(bgimage);    //创建临时纹理数据缓冲区    void *data = malloc(width * height * 4);    //将图片绘制到缓冲区中    CGContextRef ctx = CGBitmapContextCreate(data,                                             width,                                             height,                                             8,                                             width * 4,                                             CGImageGetColorSpace(image.CGImage),                                             kCGImageAlphaPremultipliedLast);    CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage);    CGFloat lineWidth = 4.0; //线宽    CGContextSetRGBStrokeColor(ctx,205,193,193,1.0);//画笔线的颜色    CGContextSetLineWidth(ctx, lineWidth);//线的宽度    // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。    CGContextAddArc(ctx, width/2, width/2, width/2-lineWidth/2, 0, 2*3.14159265358979323846, 0); //添加一个圆    CGContextDrawPath(ctx, kCGPathStroke); //绘制路径    //绘制    CGContextStrokePath(ctx);    //将其绘制到新的图片上    CGImageRef cgimage = CGBitmapContextCreateImage(ctx);    UIImage *newImage = [UIImage imageWithCGImage:cgimage];    CFRelease(cgimage);    CGContextRelease(ctx);    return newImage;}


0 0
原创粉丝点击