//设置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. MVC新手指南

    MVC新手指南 2010-04-06 09:54:23 18839 次阅读 0 条评论   本文感谢东西提供 模型-视图-控制器(MVC)可能是近年来网络编程圈子里最常被提及的模式之一.目前与网络应用 ...

  2. qt练习10 涂鸦板源代码

    源代码下载: http://files.cnblogs.com/hnrainll/doodle.zip http://www.cnblogs.com/hnrainll/archive/2011/05/ ...

  3. dreamweaver cs5中提示扩展管理不可用

    下载: Extension Manager CS5.5 for Windows 安装后重启就能用了

  4. ThinkPHP:读取数据库数据 (2)

    项目配置文件Conf/config.php中添加数据库连接信息: // 添加数据库配置信息 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'loc ...

  5. PowerDesiger 15逆向生成工程E-R图及导出word表格

    应用环境:win8(64位)+oracle10g(32位)服务端+PowerDesigner15 需求:oracle数据库中的表结构是web工程框架hibernate 自动生成,现需要将数据库中已有的 ...

  6. SSIS ->> 通过Checkoints实现SSIS的包重启(Package Restartability Fullfilled By Checkoints)

    Checkpoints是实现SSIS包重载的基础.它的原理是把当前运行环境的配置.变量以及到了包运行到哪一步和哪一步失败这些信息写入到一个文件中.而且有一点很重要,Checkpoint只发生在Cont ...

  7. Linux平台Cpu使用率的计算

    proc文件系统 /proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为内核与进程提供通信的接口.用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的 ...

  8. zf2 安装

    下载实例程序 ZendSkeletonApplication 解压至D:\xampp\htdocs并重命名为ZendSkeletonApplication 下载Zend Framework 2.0最新 ...

  9. SNMP SNMP协议

    SNMP协议 一.什么是SNMP? SNMP是简单网络管理协议[Simple Network Management Protocol],由一组网络管理的标准组成,包含一个应用层协议(applicati ...

  10. Linux系统时间与RTC时间【转】

    http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3637782 Linux的RTC驱动相对还是比较简单的,可以将它作为一个普通的字符 ...