****

#import "HMViewController.h"

#import "UIImage+Tool.h"

@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // _imageView.image = [UIImage imageWithName:@"阿狸头像" border:10 borderColor:[UIColor redColor]]; [self clipCircle]; } - (void)clipCircle
{
// 圆环的宽度
CGFloat borderW = ; // 加载旧的图片
UIImage *oldImage = [UIImage imageNamed:@"阿狸头像"]; // 新的图片尺寸
CGFloat imageW = oldImage.size.width + * borderW;
CGFloat imageH = oldImage.size.height + * borderW; // 设置新的图片尺寸
CGFloat circirW = imageW > imageH ? imageH : imageW; // 开启上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0); // 画大圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , circirW, circirW)]; // 获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 添加到上下文
CGContextAddPath(ctx, path.CGPath); [[UIColor whiteColor] set]; //边框颜色 // 渲染
CGContextFillPath(ctx); CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height); // 画圆:正切于旧图片的圆
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR]; // 设置裁剪区域
[clipPath addClip]; // 画图片
[oldImage drawAtPoint:CGPointMake(borderW, borderW)]; // 获取新的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文
UIGraphicsEndImageContext(); _imageView.image = newImage;
} - (void)clip
{
// 1.加载旧的图片
UIImage *oldImage = [UIImage imageNamed:@"阿狸头像"]; // 2.开启上下文
UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0); // 3.画圆:正切于上下文
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , oldImage.size.width, oldImage.size.height)]; // 4.设为裁剪区域
[path addClip]; // 5.画图片
[oldImage drawAtPoint:CGPointZero]; // 6.生成一个新的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 7.关闭上下文
UIGraphicsEndImageContext();
}
@end

*********************工具类

#import "UIImage+Tool.h"

@implementation UIImage (Tool)

+ (instancetype)imageWithCaptureView:(UIView *)view
{
// 开启上下文
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0); // 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 渲染控制器view的图层到上下文
// 图层只能用渲染不能用draw
[view.layer renderInContext:ctx]; // 获取截屏图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文
UIGraphicsEndImageContext(); return newImage;
} + (instancetype)imageWithName:(NSString *)name border:(CGFloat)border borderColor:(UIColor *)color
{
// 圆环的宽度
CGFloat borderW = border; // 加载旧的图片
UIImage *oldImage = [UIImage imageNamed:name]; // 新的图片尺寸
CGFloat imageW = oldImage.size.width + * borderW;
CGFloat imageH = oldImage.size.height + * borderW; // 设置新的图片尺寸
CGFloat circirW = imageW > imageH ? imageH : imageW; // 开启上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0); // 画大圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , circirW, circirW)]; // 获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 添加到上下文
CGContextAddPath(ctx, path.CGPath); // 设置颜色
[color set]; // 渲染
CGContextFillPath(ctx); CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height); // 画圆:正切于旧图片的圆
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR]; // 设置裁剪区域
[clipPath addClip]; // 画图片
[oldImage drawAtPoint:CGPointMake(borderW, borderW)]; // 获取新的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文
UIGraphicsEndImageContext(); return newImage;
} @end

IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)的更多相关文章

  1. 完美解决scrollView 截屏图片模糊

    UIGraphicsBeginImageContext   首先说明一下UIGraphicsBeginImageContextWithOptions 和UIGraphicsBeginImageCont ...

  2. vue中使用html2canvas及解决html2canvas截屏图片模糊问题

    最近在项目中用到了html2canvas插件,遇到的一些坑写下来,与大家共勉. html2canvas  官方网站http://html2canvas.hertzen.com/index.html 这 ...

  3. pytest框架优化——将异常截屏图片加入到allure报告中

    痛点分析: 在做allure定制化的时候,关于附件添加这一块,我们在代码里可以添加我们准备好的附件,这里用图片,通过下面的方法就能实现 allure.attach(file, '图片描述', allu ...

  4. Selenium截屏 图片未加载的问题解决--【懒加载】

    需求: 截屏后转PDF. 问题: selenium截屏后,图片未加载 如下图: 原因: 网站使用了懒加载技术:只有在浏览器中纵向滚动条滚动到指定的位置时,页面的元素才会被动态加载. 什么是图片懒加载? ...

  5. 获取camera截屏图片

    Camera camera; SpriteRenderer sprRender; Texture2D t2d = New Texture2D(1300, 760, TextureFormat.RGB2 ...

  6. cocos2dx之保存截屏图片

    http://blog.csdn.net/ganpengjin1/article/details/19088921 我们要保存当前的运行的scene的截图的话,我用到CCRenderTexture,看 ...

  7. [Egret]长按截屏分享、分享截屏图片、本地存储

    egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...

  8. uni-app学习记录04-轮播图和滑屏图片

    <template> <view> <!-- 轮播图视图 swiper-item是每页的元素 --> <swiper :indicator-dots=&quo ...

  9. iOS检测用户截屏并获取所截图片

    iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...

随机推荐

  1. 理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序

    理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序 在CSS中,a标签有4种伪类,分别为: a:link, a:visited, a:hover, a:active 对其稍有了解的前端er都 ...

  2. [工作中的设计模式]适配器模式adapter

    一.模式解析 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 也就是说,如果已经写好了一个接口,但是又来了一种截然不同的接口,如 ...

  3. 3、利用SuperObject 循环处理Json深层次的值

    //遍历对象 procedure TForm1.Button5Click(Sender: TObject); var item,jo: ISuperObject; ja,JA_TYPE,JA_MAC: ...

  4. 【转】linux network namespace 学习

    原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...

  5. [转载] Spring MVC - 处理器拦截器

    5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器)类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.   ...

  6. WPF: 旋转Thumb后,DragDelta移动距离出错的解决

    当Thumb跟随Grid旋转90度后,拖拽控件时会飞掉. <Grid x:Name="gridMain" Width="100" Height=" ...

  7. 【Linux程序设计】之进程间的通信

    这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的. 实验题目:Linux环境下的进程间通信 实验目的:熟悉进程通信中信号概念及信号处理:掌握进程间的管道通信编程 ...

  8. BZOJ4504 : K个串

    从左往右枚举右端点,用一棵线段树维护每个左端点的去重后的区间和. 那么对于$a[r]$,需要在$[pre[a[r]]+1,r]$里区间加上$a[r]$. 将线段树可持久化,并维护区间最大值,就可以在线 ...

  9. BZOJ3551 : [ONTAK2010]Peaks加强版

    首先强制在线的话,肯定是不能再离线排序+平衡树启发式合并了. 这回要用的是线段树合并,每次把两棵线段树合并,总复杂度为$O(n\log n)$ 预处理: 把边按权值从小到大排序,依次加边, 对于边(x ...

  10. HDU 2831 (贪心)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2831 题目大意:植物大战僵尸.给定种植植物时间间隔t,以及每个僵尸的到达时间v,生命d.问是否能赢. ...