iOS 画圆图片的几种方法
方法一:
self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[self.view addSubview:self.cycleImv];
// 为图片切圆
self.cycleImv.layer.masksToBounds = YES;
self.cycleImv.layer.cornerRadius = self.cycleImv.frame.size.width / 2.0;
// 为图片添加边框,根据需要设置边框
self.cycleImv.layer.borderWidth = 2.0;//边框的宽度
self.cycleImv.layer.borderColor = [UIColor redColor].CGColor;//边框的颜色
方法二:
- (void)drawRect:(CGRect)rect
{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.cycleImv.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.cycleImv.bounds.size];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
//设置大小
maskLayer.frame = self.cycleImv.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
self.cycleImv.layer.mask = maskLayer;
}
方法三:
将网络图片裁剪为圆形,首先建立一个UIImage分类UIImage+Extension,一个UIImageView分类UIImageView+CircularImv。
UIImage+Extension.h文件
#import@interface UIImage (Extension)
- (UIImage *)circleImage;
@end
UIImage+Extension.m文件
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
- (UIImage *)circleImage
{
// 开始图形上下文,NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置一个范围
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
// 根据一个rect创建一个椭圆
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将原照片画到图形上下文
[self drawInRect:rect];
// 从上下文上获取剪裁后的照片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭上下文
UIGraphicsEndImageContext();
return newImage;
}
@end
使用了SDWebImage加载网络图片,所以加上UIImageView+WebCache.h头文件。
UIImageView+CircularImv.h文件
#import@interface UIImageView (CircularImv)
- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName;
@end
UIImageView+CircularImv.m文件
#import "UIImageView+CircularImv.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Extension.h"
@implementation UIImageView (CircularImv)
- (void)setCircularImvURL:(NSString *)imageUrl holderImageName:(NSString *)imageName
{
//占位图片,当URL上下载的图片为空,就显示该图片
UIImage *placeholder = [[UIImage imageNamed:imageName] circleImage];
[self sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//当图片下载完会来到这个block,执行以下代码
self.image = image ? [image circleImage] : placeholder;
}];
}
@end
iOS 画圆图片的几种方法的更多相关文章
- 像画笔一样慢慢画出Path的三种方法(补充第四种)
今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- (转载)ios关闭虚拟键盘的几种方法
在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法.这个需要我们自己去实现.这三类视图对象分别是UITextField,UITextView和U ...
- iOS 创建单例的两种方法
创建一个单例很多办法.我先列举一个苹果官方文档中的写法. [cpp] view plaincopy static AccountManager *DefaultManager = nil; + (Ac ...
- 【转】IOS中Json解析的四种方法
原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有 ...
- IOS中延迟执行的几种方法
前几天去国美在线面试,就遇到了上面的问题,当时是笔试,只写出来了第一种方法,现在整理了一下. //1.performSelector方法:在当前线程中执行的方法,使用默认模式,并延迟执行@select ...
- iOS 清理缓存功能实现第一种方法
添加一个提示框效果导入第三方MBProgressHUD #import "MBProgressHUD+MJ.h" /** * 清理缓存第一种方法 */ -(void)clearCa ...
- iOS获取网络类型的四种方法
Reachability类只能区分WIFI和WWAN类型,却无法区分2G网和3G网. 网上也有些方法,却都存在Bug. 经过网上查找资料和测试,基本上总结了以下几种方法: 1.使用导航栏的方式:(私有 ...
- ios动态添加属性的几种方法
http://blog.csdn.net/shengyumojian/article/details/44919695 在ios运行过程中,有几种方式能够动态的添加属性. 1-通过runtime动态关 ...
随机推荐
- ViewerJS 一个在浏览器上查看 PDF 和电子表格的 JavaScript 库
Viewer.js简介 http://viewerjs.org/ 下载Viewer.js压缩包,解压后将ViewerJS文件夹放在网站根目录下 在浏览器地址栏中输入网址http://172.16.8. ...
- URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) 有时候在IDEA中配置spring文件 ...
- 1.8 Hive运行日志配置和查看
一.配置文件 1.重命名配置文件 # 把/opt/modules/hive-0.13.1/conf/hive-log4j.properties.template重命名为hive-log4j.prope ...
- ElasticSearch入门及核心概念介绍
Elasticsearch研究有一段时间了,现特将Elasticsearch相关核心知识和原理以初学者的角度记录下来,如有不当,烦请指正! 0. 带着问题上路——ES是如何产生的? (1)思考:大 ...
- sed的基础用法简介
sed 最近学习了一些sed的相关知识,初步接触sed以后给我的感受主要有两点.首先是sed强大的功能,学了以后发现之前写的脚本利用sed以后会简化很多啊,具体的有些利用sed编辑shell脚本的思路 ...
- pre 自动换行
pre { white-space:pre-wrap; word-wrap:break-word; } 增加那么一句即可!
- 535. Encode and Decode TinyURL(rand and srand)
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- Shader第十三讲 Alpha混合
http://blog.sina.com.cn/s/blog_471132920101d8z5.html Alpha Blending,中文译作Alpha混合Blending就是控制透明的.处于光栅化 ...
- UnityEngine中Animator相关类的说明
---------------------------------------------------------------------- Animator 这个单独写,比较多 AnimationC ...
- Unity中资源动态加载的几种方式比较
http://blog.csdn.net/leonwei/article/details/18406103 初学Unity的过程中,会发现打包发布程序后,unity会自动将场景需要引用到的资源打包到安 ...