美文网首页
两种方式裁剪带外环的图片

两种方式裁剪带外环的图片

作者: blockstrong | 来源:发表于2018-08-24 15:07 被阅读0次

    1.-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {

        UIGraphicsBeginImageContext(image.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2);

        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);

        CGRectrect =CGRectMake(0,0, image.size.width, image.size.height);

        CGContextAddEllipseInRect(context, rect);

        CGContextClip(context);

        [imagedrawInRect:rect];

        CGContextAddEllipseInRect(context, rect);

        CGContextStrokePath(context);

        UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        returnnewimg;

    }

    2.

    -(UIImage*)circleImage:(UIImage*)image withBorder:(CGFloat)border

    {

        CGFloatimageWH = image.size.width;

        //大圆形的宽度高度

        CGFloatovalWH = imageWH +2*border;

        //1、开启位图上下文

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH,ovalWH), NO, 0);

        //2、画大圆

        UIBezierPath *path =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];

        [[UIColor greenColor]set];

        [pathfill];

        //3、设置裁剪区(小圆)

        UIBezierPath *clipPath =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];

        [clipPathaddClip];

        //、绘制图片

        [imagedrawAtPoint:CGPointMake(border, border)];

        //、获取图片

        UIImage *clipImage=UIGraphicsGetImageFromCurrentImageContext();

        //、关闭上下文

        UIGraphicsEndImageContext();

        returnclipImage;

    }

    相关文章

      网友评论

          本文标题:两种方式裁剪带外环的图片

          本文链接:https://www.haomeiwen.com/subject/clobiftx.html