//设置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. 设置 Firewalld 防火墙控制对系统的访问

    1.检查 server101 的网卡,记住第二个网卡的名称 [root@server101 ~]# nmcli device DEVICE TYPE STATE CONNECTION br0 brid ...

  2. URAL 1233 Amusing Numbers 好题

    参照了nocow上的解法,照搬过来…… 易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561: 5位上:10000~48560; 4位上:10 ...

  3. R语言学习笔记——Base Graphics

    做exploratory data annalysis的作业,差点被虐死了,R从头开始,边做边学,最后搞到一点多才弄完,还有一个图怎么画都不对,最后发现是数据读取的时候有问题. 用来画图的数据来自:h ...

  4. UBoot常用命令手册

    UBoot常用命令手册 U-Boot还提供了更加详细的命令帮助,可以通过”?”显示支持的命令列表,通过help [CommandName]命令还可以查看每个命令的参数说明. 1.bootm bootm ...

  5. SPA与DPA 攻击【转】

    转自:http://blog.sina.com.cn/s/blog_6cb58dbf0102v7ym.html SPA SPA是一种直接解释能量消耗测定值的技术.系统消耗能量的大小随微处理器执行的指令 ...

  6. (5)XML转化成TXT

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  7. 《OD学hadoop》Hadoop前置

    一.Hadoop 前置课程 1. Linux系统,基本命令 2. Java语言,JavaSE相关知识 3. MySQL基本的DML和DDL SQL on Hadoop

  8. Searching in a Radius using Postgres[Marked]

    Searching in a Radius using Postgres Creating a GEO application has never been easier. You can have ...

  9. 基于Linux的oracle数据库管理 part2( 数据库 准备,安装,创建 )

    主要内容 1. 准备 2. 安装 与 删除 软件 3. 创建数据库 4. 配置 SQL*PLUS 环境 准备 1. 软件包, rpm –qa , rpm –ivh *.rpm 2. 检查磁盘空间 3. ...

  10. 浅析JavaScript之Function类型

    JavaScript中的函数实际上是对象,每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上只是指向函数对象的指针,保存函数在堆内存中的地 ...