IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
****
#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图片剪裁变圆行图,和截屏图片)的更多相关文章
- 完美解决scrollView 截屏图片模糊
UIGraphicsBeginImageContext 首先说明一下UIGraphicsBeginImageContextWithOptions 和UIGraphicsBeginImageCont ...
- vue中使用html2canvas及解决html2canvas截屏图片模糊问题
最近在项目中用到了html2canvas插件,遇到的一些坑写下来,与大家共勉. html2canvas 官方网站http://html2canvas.hertzen.com/index.html 这 ...
- pytest框架优化——将异常截屏图片加入到allure报告中
痛点分析: 在做allure定制化的时候,关于附件添加这一块,我们在代码里可以添加我们准备好的附件,这里用图片,通过下面的方法就能实现 allure.attach(file, '图片描述', allu ...
- Selenium截屏 图片未加载的问题解决--【懒加载】
需求: 截屏后转PDF. 问题: selenium截屏后,图片未加载 如下图: 原因: 网站使用了懒加载技术:只有在浏览器中纵向滚动条滚动到指定的位置时,页面的元素才会被动态加载. 什么是图片懒加载? ...
- 获取camera截屏图片
Camera camera; SpriteRenderer sprRender; Texture2D t2d = New Texture2D(1300, 760, TextureFormat.RGB2 ...
- cocos2dx之保存截屏图片
http://blog.csdn.net/ganpengjin1/article/details/19088921 我们要保存当前的运行的scene的截图的话,我用到CCRenderTexture,看 ...
- [Egret]长按截屏分享、分享截屏图片、本地存储
egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...
- uni-app学习记录04-轮播图和滑屏图片
<template> <view> <!-- 轮播图视图 swiper-item是每页的元素 --> <swiper :indicator-dots=&quo ...
- iOS检测用户截屏并获取所截图片
iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...
随机推荐
- Uva 839 Not so Mobile
0.最后输出的yes no的大小写 1.注意 递归边界 一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...
- WPF三种基本触发器与【与或】逻辑触发器
wpf中的触发器是应用于程序界面模板.样式.皮肤.主题的基础.以下作为学习的记录. 1,三种基本触发器,属性触发器.数据触发器.事件触发器 属性触发器 <!--属性触发器--> <T ...
- 服务器控件和 viewstate
//不会产生处理回发事件的方法.类似客户端html Repeater rep = new Repeater(); DataList dtl = new DataList(); FileUpload f ...
- Codeforces Round #349
终于又回到熟悉的Round了 数学 A - Pouring Rain 设个未知数,解方程,还好没有hack点 #include <bits/stdc++.h> typedef long l ...
- MFC 打开文件夹选择框并获取文件夹路径
CString FicowGetDirectory() { BROWSEINFO bi; char name[MAX_PATH]; ZeroMemory(&bi, sizeof(BROWSEI ...
- UIImage两种初始化的区别
UIImage可以通过以下两种方式进行初始化: //第一种初始化方式:[注意使用这种初始化的时候如果是png格式的可以不给后缀名,根据屏幕的的分辨率去匹配图片] UIImage *image = [U ...
- vwampserver2.5-apache2.4.9允许外部访问的配置
打开..\wamp\bin\apache\apache2.4.9\conf\httpd.conf配置文件, <Directory "c:/wamp/www/"> ...
- document.forms[0].submit object is not a function
今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. ...
- 显式Intent和隐式Intent
http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...
- RequireJS 循环依赖报 模块undefined 处理方案
RequireJS 循环依赖 开始学习使用RequireJS之后做了几个小例子,之后想着把手头的项目也用RequireJS写一遍试试.感觉胜利就在前方了,忽然发现始终卡在一个问题上: 很常见的一个问题 ...