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动态关 ...
随机推荐
- hdu 3932 Groundhog Build Home —— 模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 找一个位置使距离最远的点的距离最小: 上模拟退火: 每次向距离最远的点移动,注意判断一下距离最远的点 ...
- SQL Server中查询CPU占用高的SQL语句
SQL Server中查询CPU占用高的情况,会用到sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests 一.查看当前的数据库用户连接有多少 USE ...
- Gibonacci number-斐波那契数列
Description In mathematical terms, the normal sequence F(n) of Fibonacci numbers is defined by the r ...
- python---socket与socketserver
1.socket的方socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0) #获取要连接的对端主机地址sk.bind(ad ...
- react中虚拟DOM的基本概念
react中的核心概念 1.DOM的本质是什么: 浏览器中的概念,用js对象来表示页面上的元素,并提供操作DOM对象的API 2.什么事react中的虚拟DOM:是框架中的概念,是程序员用js对象来模 ...
- 项目中常用的js骚操作
//打开网址window.open("http://www.runoob.com"); //判断是否为url var url = $("#url").val() ...
- Lightoj1080 【线段树】
题意: 给你一个0/1的数组,然后给你n段区间,说这个区间里要反转一次,然后给你Q个询问,问你这个位置是什么: 思路: 我们线段树维护一下就好了额: 其实反转的话,还是算次数是不是,奇偶嘛: #inc ...
- P5175 数列(矩阵加速)
//minamoto #include<bits/stdc++.h> #define R register #define ll long long #define fp(i,a,b) f ...
- [Xcode 实际操作]九、实用进阶-(17)使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色
目录:[Swift]Xcode实际操作 本文将演示如何使用CGBlendMode改变UIImage颜色,实现对图片进行混合着色. 在项目文件夹[DemoApp]上点击鼠标右键 ->[New Fi ...
- mybits like查询写法
1.mysql :LIKE CONCAT('%',#{empname},'%' ) 或者 LIKE CONCAT('%',‘${empname}’,'%' ) 2.oracle:LIKE '%'||# ...