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动态关 ...
随机推荐
- Mysql源码学习——Thread Manager
一.前言 上篇的Connection Manager中,曾提及对于一个新到来的Connection,服务器会创建一个新的线程来处理这个连接. 其实没那么简单,为了提高系统效率,减少频繁创建线程和中止线 ...
- for循环的一个注意点
unsigned int i =10; for(i;i > 0; i--) { xxxxx } 因为i是unsigned int 类型的,永远不可能小于0,也就是说是个死循环了.
- tabbar
1 tabbar不显示的问题: 命名在app.json中配置了tabbar但是不显示可能的问题: app.json中的pages中第一个路径没有在tabbar中设置 原因:app.json中配置的pa ...
- opencv 笔记
http://docs.opencv.org/ opencv 2.x API opencv包含以下模块 core 基本数据机构 imgproc 图像处理方法 video 视频处理方法 ...
- Eigen中的noalias(): 解决矩阵运算的混淆问题
作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...
- 【Hadoop】MapReduce笔记(三):MapReduce的Shuffle和Sort阶段详解
一.MapReduce 总体架构 整体的Shuffle过程包含以下几个部分:Map端Shuffle.Sort阶段.Reduce端Shuffle.即是说:Shuffle 过程横跨 map 和 reduc ...
- SQL Server知识详解
1.SET NOCOUNT ON的作用: 作用:阻止在结果集中返回显示受T-SQL语句或则usp影响的行计数信息. 语法:SET NOCOUNT {ON | OFF} 详解:当SET ONCOUNT ...
- E20190215-mt
parenthesis n. 圆括号; 插入语; 插入成分; 间歇; (parentheses) individual adj. 个人的; 个别的; 独特的; n. 个人; 个体; priva ...
- 动态加载dll
extern "C" MMUPDATENOTIFY_IMPEXP bool _cdecl NotifyThrift(char* chThriftIp, char* chPor) H ...
- Lightoj1003【判环操作】
题意: 对于n个给出字符串a,b,理解成a在b之前办好这个事情,要求n个给出两个串,a都要在b之前完成: 题意: 所以一旦出现环就不行了: 以前在写最短路的时候,spfa就有一个判环,后来写着写着写到 ...