****

#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. 使用asynctask的问题

    在使用asynctask  的onpostexcute 这个方法的时候,发现return 变量这个语句,只是return 到onpostexcute 本来 我是 private 变量1 方法1(){ ...

  2. WPF中ControlTemplate和DataTemplate的区别

    下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...

  3. ASP.NET Global.asax详解

    最近在研究bbsmax的代码,但是一直不知道入口在哪里,然后就对各个文件分析了,然后终于在对global.asax文件查看的时候看到Application_BeginRequest才明白入口,所以现在 ...

  4. 微软曝光眼球追踪新专利,未来或将可以使用眼球控制HoloLens

    想要在增强现实(AR)和虚拟现实(VR)中获得感官能力,计算机的配合非常重要.如何能够更好的追踪我们所感知的,计算机判断用户目光所向的能力就必须进一步提高.根据微软新曝光的专利,可能已经有一个解决方案 ...

  5. Window.location

    1.location 对象 // 假设当前url是 http://localhost/rpc/plugin.php#hash?a=aaa&b=bbb alert(window.location ...

  6. js判断访问来源

    通过navigator的userAgent属性来判定 userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值.一般来讲,它是在 navigator.appCode ...

  7. 【wikioi】1003 电话连线

    题目链接 算法: 最小生成树 PS:被卡过2天(中间的时间没去做).日期:2013-09-13 13:49:47 ~ 2013-09-17 13:01:07 此题为基础题 刚开始学图论时只会用Krus ...

  8. IEqualityComparer<T>

    在linq中使用union和distinct都不起作用,结果发现必须传入一个实现了IEqualityComparer<T>的比较器 public class CompareUser : I ...

  9. iOS 开发小结

    一,经历 1> 在编写以前有过的类似的新功能时,如果以前的开发人员没有写明明确的注释和开发需求,一定要仔细阅读所有代码,每一句代码都有它存在的意义. 2> 例如,只以为是[self.ful ...

  10. thinkphp 联表查询,排序

    $info =M('productbase'); $info= $info->alias('a')->field('a.id,cid,title,address,protype,time, ...