把图片切成圆角代码
来源:互联网 发布:易语言论坛源码 编辑:程序博客网 时间:2024/05/11 01:39
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, 10, 10);
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];
}
直接调用createRoundedRectImage....
返回圆角图片
圆角大小自行修改CGContextAddArcToPoint....
转帖 : 原文网址 http://www.cocoachina.com/bbs/read.php?tid-1757-keyword-%CD%BC%C6%AC.html
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角的代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 把图片切成圆角的代码
- 把图片切成圆角代码
- 把图片切成圆角代码
- 【ios】把图片切成圆角代码 实现及问题
- 用Photoshop把图片切成圆角
- 把图片切成小片(IOS)
- 将图片切成圆角,包括圆形
- Android中把矩形图片切成圆形图片
- 420_把bitmap切成圆的
- VSS 配置操作手册
- (优化的有限背包) poj 1742 Coins(转载)
- double转为百分比的类
- 网页编辑器大全
- 面向对象设计的11原则
- 把图片切成圆角代码
- 阿甘正传 和 肖申克的救赎的经典台词
- VS下 inline函数链接报错:LNK2019
- 阿甘正传 和 肖申克的救赎的经典台词
- 背包九讲(转载)
- 优秀前端设计网站推荐
- Java的反射机制
- 我的处女博文——乱78糟的结束,新的开始
- 使用DUMP 文件调试分析内核驱动