开发笔记 那些年追过的图片(十) 图片处理成圆形


[plain]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIImage (Rounded)  
  4.   
  5. + (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r;  
  6.   
  7. @end  


[plain]  view plain copy
  1. #import "UIImage+Rounded.h"  
  2.   
  3. @implementation UIImage (Rounded)  
  4.   
  5. static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,  
  6.                                  float ovalHeight)  
  7. {  
  8.     float fw, fh;  
  9.       
  10.     if (ovalWidth == 0 || ovalHeight == 0)  
  11.     {  
  12.         CGContextAddRect(context, rect);  
  13.         return;  
  14.     }  
  15.       
  16.     CGContextSaveGState(context);  
  17.     CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));  
  18.     CGContextScaleCTM(context, ovalWidth, ovalHeight);  
  19.     fw = CGRectGetWidth(rect) / ovalWidth;  
  20.     fh = CGRectGetHeight(rect) / ovalHeight;  
  21.       
  22.     CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner  
  23.     CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner  
  24.     CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner  
  25.     CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner  
  26.     CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right  
  27.       
  28.     CGContextClosePath(context);  
  29.     CGContextRestoreGState(context);  
  30. }  
  31.   
  32. + (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r  
  33. {  
  34.     // the size of CGContextRef  
  35.     int w = size.width;  
  36.     int h = size.height;  
  37.       
  38.     UIImage *img = image;  
  39.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
  40.     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);  
  41.     CGRect rect = CGRectMake(0, 0, w, h);  
  42.       
  43.     CGContextBeginPath(context);  
  44.     addRoundedRectToPath(context, rect, r, r);  
  45.     CGContextClosePath(context);  
  46.     CGContextClip(context);  
  47.     CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);  
  48.     CGImageRef imageMasked = CGBitmapContextCreateImage(context);  
  49.     img = [UIImage imageWithCGImage:imageMasked];  
  50.       
  51.     CGContextRelease(context);  
  52.     CGColorSpaceRelease(colorSpace);  
  53.     CGImageRelease(imageMasked);  
  54.       
  55.     return img;  
  56. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值