你不知道的UIImageView设置成图形的两种方案
来源:互联网 发布:物流网络形式 编辑:程序博客网 时间:2024/06/08 17:58
自从iOS7出来之后,越来越多的应用头像设计成圆形了,圆形设计的头像看起来确实挻上流的。现在我给大家带来两种把UIImageView的图片设计成圆形。
方案一:从UIImageView的布局着手
我相信大家都知道对UIImageView的layer设置方法来把图片设计成圆形。
self.avatarImageView = [[ABELImageView alloc] init];self.avatarImageView.frame = CGRectMake(15, 6, 48, 48);self.avatarImageView.image = [UIImage imageNamed:@"DefaultAvatar.png"];self.avatarImageView.layer.masksToBounds = YES;// 这个必须设置为YES[self.avatarImageView.layer setCornerRadius:24.0];
方案二:从UIImage着手
我们想要图片显示成圆形,只要把UIImage弄成圆形就可以了。
UIImage的category
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 radius:(NSInteger)radius{ // the size of CGContextRef int width = size.width; UIImage *img = image; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, width, width, 8, 4 * width, colorSpace, 2); //2 = kCGImageAlphaPremultipliedFirst CGRect rect = CGRectMake(0, 0, width, width); CGContextBeginPath(context); addRoundedRectToPath(context, rect, radius, radius); CGContextClosePath(context); CGContextClip(context); CGContextDrawImage(context, CGRectMake(0, 0, width, width), img.CGImage); CGImageRef imageMasked = CGBitmapContextCreateImage(context); img = [UIImage imageWithCGImage:imageMasked]; CGContextRelease(context); CGColorSpaceRelease(colorSpace); CGImageRelease(imageMasked); return img;}
实际应用
self.avatarImageView = [[ABELImageView alloc] init];self.avatarImageView.frame = CGRectMake(15, 6, 48, 48);self.avatarImageView.image = [UIImage createRoundedRectImage:image size:image.size radius:image.size.width / 2.0];
两种方法对比:第一种比较简单,也是大家比较常用的方法,但对用户体验不好,运行在UITableView上的时候会有一点卡的效果,当然对于不是要求特别高的应用可以忽略这一点。第二种是通过低层的绘图方式,优点是效率好,不会存在卡的效果,相对来说有一定的难度。
1 0
- 你不知道的UIImageView设置成图形的两种方案
- java线程两种实现方式的区别,你所不知道的小细节
- java线程两种实现方式的区别,你所不知道的小细节
- Win8中那个你不知道的小设置
- Android偏好设置SharedPreference你不知道的秘密
- 你不知道的事
- 你不知道的QQ
- 【你不知道的Jetty】
- 你不知道的AS
- 你不知道的shell
- UIView 你不知道的
- 你不知道的事
- 你不知道的历史
- 你不知道的事。
- 你不知道的事
- 你不知道的事
- 你不知道的 HTTP
- 你不知道的 collider
- fzu 2037 Maximum Value Problem(递推)
- win7 下搭建基于jdk1.7 android开发环境
- 索引结构
- python常用的第三方库
- 梯度下降法
- 你不知道的UIImageView设置成图形的两种方案
- ./configure时候遇到的问题 Cannot find install-sh, install.sh, or shtool in ac-aux
- 信息检索之布尔检索
- ios代码行数统计
- VI编辑器常用快捷键
- C++11的语法新特性
- AMR音频文件格式分析
- poj 3669 Meteor Shower
- VS2013中的C++11新特性