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之前, 如果用户截屏 ...
随机推荐
- no-jquery 03 Ajax
Ajax Requests GETting var xhr = new XMLHttpRequest(); xhr.open('GET', encodeURI('myservice/username? ...
- 工具:使用jekyll生成静态网站
在使用前端框架构建网页而不使用后端平台与数据库,即没有服务器的条件下读取文件夹其他文件,浏览器可能会阻止访问.对于这种静态构建可以使用简单的生成工具jekyll.它主要适用于将静态文件生成静态网站,在 ...
- js三级省市区选择
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- A - The Moronic Cowmpouter
Description Inexperienced in the digital arts, the cows tried to build a calculating engine (yes, it ...
- [转] FastMM使用详解
FastMM使用详解 一.引言 FastMM 是适用于delphi的第三方内存管理器,在国外已经是大名鼎鼎,在国内也有许多人在使用或者希望使用,就连 Borland 也在delphi2007 ...
- hdfs 集群间拷贝
hadoop distcp -i hdfs://192.168.10.211:9000/fileinfo hdfs://192.168.24.46:9000/fileinfo distcp [OPTI ...
- Codeforces 627A XOR Equation(思路)
题目大概说两个正整数a.b,已知s=a+b以及x=a xor b的值,问有几种a.b这样的数对. 我知道异或相当于无进位的加法,s-x就是其各个位置的进位,比如s-x=1010,那就表示a和b的第1位 ...
- eclipse 高亮配对的括号
在编辑代码框右键->preference,直接就可以看到Matching brackets highlights
- Android 解压缩功能
主要用到zip: import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedOutpu ...
- Leetcode Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...