//设置UIImage圆角

@interface UIImage(UIRoundedRectImage)

+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size;

@end

@implementation UIImage(UIRoundedRectImage)

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,

float ovalHeight)

{

float fw,fh;

if (ovalWidth == 0 || ovalHeight == 0) {

CGContextAddRect(context, rect);

return;

}

CGContextSaveGState(context);

CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

CGContextScaleCTM(context, ovalWidth, ovalHeight);

fw = CGRectGetWidth(rect) / ovalWidth;

fh = CGRectGetHeight(rect) / ovalHeight;

CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner

CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner

CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner

CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right

CGContextClosePath(context);

CGContextRestoreGState(context);

}

+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size

{

// the size of CGContextRef

int w = size.width;

int h = size.height;

UIImage *img = image;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace,kCGImageAlphaPremultipliedFirst);

CGRect rect = CGRectMake(0, 0, w, h);

CGContextBeginPath(context);

addRoundedRectToPath(context, rect, 5, 5);

CGContextClosePath(context);

CGContextClip(context);

CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

CGImageRef imageMasked = CGBitmapContextCreateImage(context);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

return [UIImage imageWithCGImage:imageMasked];

}

@end

设置UIImage 圆角的更多相关文章

  1. tableView 获取网络图片,并且设置为圆角(优化,fps)

    代码地址如下:http://www.demodashi.com/demo/11088.html 一.准备工作 例子比较精简,没有什么特殊要求,具备Xocde8.0左右版本的就好 二.程序实现 1.相关 ...

  2. 设置UIImage的渲染模式:UIImage.renderingMode

    设置UIImage的渲染模式:UIImage.renderingMode 着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重 ...

  3. iOS UITabBarItem 选中图的颜色,设置UIimage的渲染模式

    UITbarController之前有在这篇文章讲解:http://www.cnblogs.com/niit-soft-518/p/4447940.html 如果自定义了UITabBarItem的图片 ...

  4. php imagick设置图片圆角的方法

    php imagick设置图片圆角的方法 <pre>header('Content-Type: image/png'); $image = new Imagick('http://stat ...

  5. 设置UICollectionViewCell圆角和阴影

    设置cell圆角: cell.contentView.layer.cornerRadius =2.0f; cell.contentView.layer.borderWidth =1.0f; cell. ...

  6. iOS 中如何将View设置为圆角的矩形?

    今天刚好需要添加一个圆角的view. 必须先导入头文件. #import <QuartzCore/QuartzCore.h> bgView.layer.cornerRadius = cor ...

  7. [转]怎么把一个textview的背景图片设置成圆角的?

        在drawable文件夹下新建一个文件设置背景样式代码:在drawable文件夹下面新建text_view_border.xml<?xml version="1.0" ...

  8. iOS: 使用故事板和xib设置按钮圆角方法

    使用storyboard如何设置圆角或边框? 通过storyboard的 运行时属性runtime attribute,可以对Button设置圆角或者边框 1.很多人都知道,通常设置一个 Button ...

  9. ios中设置UIButton圆角,添加边框

    //例如: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(50, ...

随机推荐

  1. Java中的Enum的使用与分析

    使用name()方法和valueOf(String)方法可以在枚举类型对象和字符串之间方便得转换.如果valueOf(String)方法的参数不是该枚举类型合法的字符串,则会抛出IllegalArgu ...

  2. 测试 float

    关于blog上对 float的许多讨论,这个函数不对,那个运算出错. 其实原因不在这里,根源是计算机没有向你保证你看到的就是真实的,都是一厢情愿而已. 废话不说.下面是测试,一看就明白.再不明白的看看 ...

  3. rqnoj-106-最大加权矩形-dp

    和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...

  4. android:screenOrientation的说明 固定屏幕显示方向

    Activity在屏幕当中显示的方向.属性值可以是下表中列出的一个值: "unspecified" 默认值,由系统来选择方向.它的使用策略,以及由于选择时特定的上下文环境,可能会因 ...

  5. SeaJS 学习

    什么是系统 在生活和工作中,我们会接触到大量系统:自然界生态系统.计算机操作系统.软件办公系统,还有教育系统.金融系统.网络系统.理论系统等等.究竟什么是系统呢? 来看下维基百科的解释: 系统泛指由一 ...

  6. eclipse中tomcat使用add and remove无法发布web项目

    继上次启动eclipse中的tomcat报classNotFound的问题后,这次又遇到新问题.就是右键点击tomcat使用add and remove发布web项目至tomcat后,启动tomcat ...

  7. Python 优雅的操作字典【转】

    Python 中的字典是Python中一个键值映射的数据结构,下面介绍一下如何优雅的操作字典. 1.1 创建字典 Python有两种方法可以创建字典,第一种是使用花括号,另一种是使用内建 函数dict ...

  8. ViewState压缩技术

    ViewState 的使用,大家可以说是又爱又恨,它其中一个特性就是保存页面的状态,对于只是展示的页面,我们可以直接在页面文件中使用 EnableViewState="false" ...

  9. java 文件text的写入

    日志文件 OutputStream out = new FileOutputStream(f, true); if (list1.size() > 0) { int h1 = 0; for (i ...

  10. 无锁编程(六) - seqlock(顺序锁)

    seqlock(顺序锁) 用于能够区分读与写的场合,并且是读操作很多.写操作很少,写操作的优先权大于读操作. seqlock的实现思路是,用一个递增的整型数表示sequence.写操作进入临界区时,s ...