带有边框效果的图片裁剪

1.首先看下效果图吧

2.今天在实现这个功能时,直接将这个功能封装起来了,大家可以将代码直接拿过去使用;

   

#import <UIKit/UIKit.h>


@interface UIImage (image)


-(UIImage *)newImageWithBoradColor:(UIColor *)color boradWidth:(CGFloat)width image:(UIImage *)image;

@end


#import "UIImage+image.h"


@implementation UIImage (image)


//传入边框的颜色和边框的尺寸,以及你想裁剪的图片,生成一张裁剪好的图片

-(UIImage *)newImageWithBoradColor:(UIColor *)color boradWidth:(CGFloat)width image:(UIImage *)image{

    

    //开启图片上下文

    UIGraphicsBeginImageContextWithOptions(image.size,NO, 0.0);

    //绘制外面边框的路径

    UIBezierPath *largePath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, image.size.width, image.size.height)];

    //设置内容颜色

    [color set];

    //填充边框内部

    [largePath fill];

    

    //绘制图片路径

    UIBezierPath *smallPath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(width, width, image.size.width -2 * width, image.size.height -2 * width)];

    //剪裁路径

    [smallPath addClip];

    //渲染到上下文

    [image drawAtPoint:CGPointZero];

    //根据上面上下文,生成一张新的图片

    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

    //关闭上下文

    UIGraphicsEndImageContext();

    //返回新生成的图片

    return image = newImage;

   

}

@end


3.代码中的注释已经写得很清楚了,这个就不解释多了.;需要说明的是,这个类是继承UIImage写的一个分类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值