【ios】把图片切成圆角代码 实现及问题
2012-09-28 17:25
246 查看
方法一(原生支持,简单明了,最大的问题是在tableview里面会拖慢速度,感觉会卡):
self.imageView.layer.cornerRadius = 4;
self.imageView.layer.masksToBounds = YES;
方法二:
@implementation ImageUtils
//把图片切成圆角代码
static void addRoundedRectToPath(CGContextRef context,CGRect rect,
float ovalWidth,
float ovalHeight,float radius)
{
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, radius); // Top right corner
CGContextAddArcToPoint(context,
0, fh, 0, fh/2, radius);
// Top left corner
CGContextAddArcToPoint(context,
0, 0, fw/2,
0, radius); // Lower left corner
CGContextAddArcToPoint(context, fw,
0, fw, fh/2, radius); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(float) radius
{
// the size of CGContextRef
int w = size.width*2; //解决图像模糊问题
int h = size.height*2; //解决图像模糊问题
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 ,radius);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context,
CGRectMake(0, 0, w, h), image.CGImage);
CGImageRef imageMasked =
CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImageimageWithCGImage:imageMasked];
}
@end
参考文章:
http://www.cocoachina.com/bbs/read.php?tid=1757&page=1 http://www.cocoachina.com/bbs/read.php?tid=73051&page=2
self.imageView.layer.cornerRadius = 4;
self.imageView.layer.masksToBounds = YES;
方法二:
@implementation ImageUtils
//把图片切成圆角代码
static void addRoundedRectToPath(CGContextRef context,CGRect rect,
float ovalWidth,
float ovalHeight,float radius)
{
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, radius); // Top right corner
CGContextAddArcToPoint(context,
0, fh, 0, fh/2, radius);
// Top left corner
CGContextAddArcToPoint(context,
0, 0, fw/2,
0, radius); // Lower left corner
CGContextAddArcToPoint(context, fw,
0, fw, fh/2, radius); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(float) radius
{
// the size of CGContextRef
int w = size.width*2; //解决图像模糊问题
int h = size.height*2; //解决图像模糊问题
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 ,radius);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context,
CGRectMake(0, 0, w, h), image.CGImage);
CGImageRef imageMasked =
CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImageimageWithCGImage:imageMasked];
}
@end
参考文章:
http://www.cocoachina.com/bbs/read.php?tid=1757&page=1 http://www.cocoachina.com/bbs/read.php?tid=73051&page=2
相关文章推荐
- 发一个不用图片实现圆角代码,非常经典
- Android图片加载神器之Fresco-Java代码实现圆形圆角效果
- 图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
- iOS仿微信图片分享界面实现代码
- iOS实现高效裁剪图片圆角算法教程
- js 一行代码实现图片圆角(角度可调)+ 阴影效果实现
- iOS开发UI篇—用纯代码写实现图片轮播
- 把图片切成圆角代码
- IOS修改圆角图片问题总结
- IOS关于从照相机获取图片的问题,初步探究通过摄像头实现颜色与形状识别机制==
- WebSocket 学习笔记--IE,IOS,Android等设备的兼容性问题与代码实现
- 把图片切成圆角的代码
- 把图片切成圆角代码
- 把图片切成圆角的代码
- 把图片切成圆角代码
- IOS APP 国际化(实现不跟随系统语言,不用重启应用,代码切换stroyboard ,xib ,图片,其他资源)
- Android图片加载神器之Fresco-圆形圆角二合一[Java代码实现圆形圆角效果]
- iOS - UIScrollView控件实现图片缩放功能(还需补充代码)
- iOS实战演练之关于数据交互的问题(利用XAMPP搭建本地服务器, 编写PHP代码实现iOS端注册登录功能)