****

#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. clear both

    原文地址:http://www.codefans.net/articles/653.shtml 因CSS很多布局是需要浮动的,当属性设置float(浮动)时,其所在的物理位置已经脱离文档流了,为了使f ...

  2. hdu1114 完全背包

    题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...

  3. SU sunmo命令学习

  4. spring-boot项目在外部tomcat环境下部署

    http://m.blog.csdn.net/article/details?id=51009423

  5. http://www.cnblogs.com/meiCode/p/5896239.html

    http://www.cnblogs.com/meiCode/p/5896239.html

  6. Redis 事务总结

        特点: 对单个客户端可以执行连续性事务(在一个线程内): 执行命令要排队: mutil类似begin trans; exec 类似 commit; discard 用于放弃事务: watch ...

  7. tomcat与HTML命令提示符

    在tomcatwebapps目录下建立一个新文件夹 命名为my 把第一个学习的HTML文件放到my文件夹内 通过tomcat服务器远程访问该网页 把localhost换成自己的IP地址 先查看自己的I ...

  8. HIT2739 The Chinese Postman Problem(最小费用最大流)

    题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...

  9. POJ3084 Panic Room(最小割)

    把某点与某几点分开的最小花费,当然想到最小割.具体怎么建图,可以画个简单的情况,然后就清楚了: 0到1不受控制,建立0->1容量为INF的边: 1到0受在0一边的一个控制面板的控制,建立1-&g ...

  10. 【原】iOS学习之ARC和非ARC文件混编

    在编程过程中,我们会用到很多各种各样的他人封装的第三方代码,但是有很多第三方都是在非ARC情况下运行的,当你使用第三方编译时出现和下图类似的错误,就说明该第三方是非ARC的,需要进行一些配置.