UIImage切圆角 - 优化

UIImage等比例缩放并切圆角

如题, 在网上找的代码也是可以用的, 但是问题是切成圆角时, 如果图片本身不是正方形就会导致图片变形,被压缩或者拉伸. 今上午阅读了那一段代码, 然后自己写出了任意长宽的图片, 都可以等比例缩放并切割成圆角图片, 默认切取图像正中央的位置.

[@interface](http://my.oschina.net/u/996807) UIImage (Extension)

///将UIImage图像切成圆形的图像, 指定宽度(边长)
- (UIImage *)circleImageWithWidth:(double)width;

///切成圆形的图片
- (UIImage *)cuttingCicleImageWithSize:(CGSize)size;

///缩放图片尺寸
- (UIImage *)zoomImageToSize:(CGSize)size;

///按照比例缩放图片, 原始图片为1, 参数(0~1)
- (UIImage *)zoomImageWithScale:(float)scale;

[@end](http://my.oschina.net/u/567204)

如下, 通过- (UIImage *)circleImageWithWidth:(double)width方法进行操作的,

  • 第一步, 先将原始图片进行等比例缩放;
  • 第二步, 截取等比缩放后正中央的正方形图片;
  • 第三部, 将正方形图像切割成圆角图像.
@implementation UIImage (Extension)


///将UIImage图像切成圆形的图像, 指定宽度(边长)
- (UIImage *)circleImageWithWidth:(double)width {
    
    // 计算一些变量, 供后面使用
    float screenScale = [[UIScreen mainScreen] scale];
    float scale = width / MIN(self.size.width, self.size.height);
    CGSize originalScaleSize = CGSizeMake(self.size.width * scale, self.size.height * scale);
    CGRect centerRange = CGRectOffset(CGRectMake(0, 0, width, width), (originalScaleSize.width-width)/2, (originalScaleSize.height-width)/2);
    

    
    // 调用方法将图像缩放到指定的比例
    UIImage *newImage = [self zoomImageWithScale:scale];
    
    
    // 开始获取图片中间 正方形区域
    CGImageRef imageRef = newImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(centerRange.origin.x, centerRange.origin.y, centerRange.size.width * screenScale, centerRange.size.height * screenScale));
    newImage = [UIImage imageWithCGImage:subImageRef];
    
    
    //切成圆角并返回图像
    return [newImage cuttingCicleImageWithSize:CGSizeMake(width, width)];
}

///切成圆形的图片
- (UIImage *)cuttingCicleImageWithSize:(CGSize)size {
    
    double screenScale = [[UIScreen mainScreen] scale];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, screenScale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(ctx, rect);
    CGContextClip(ctx);
    [self drawInRect:rect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

///缩放图片尺寸
- (UIImage *)zoomImageToSize:(CGSize)size {
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

///按照比例缩放图片, 原始图片为1, 参数(0~1)
- (UIImage *)zoomImageWithScale:(float)scale {
    
    CGSize targetSize = CGSizeMake(self.size.width * scale, self.size.height * scale);
    return [self zoomImageToSize:targetSize];
}


[@end](http://my.oschina.net/u/567204)

转载于:https://my.oschina.net/whforever/blog/714972

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值