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

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;

}

你可能感兴趣的:(两种方式裁剪带外环的图片)