iOS UI进阶-1.1 Quartz2D 图片水印/裁剪/截图
图片水印
UIImage+MJ.h
#import <UIKit/UIKit.h> @interface UIImage (MJ)
/**
* 打水印
*
* @param bg 背景图片
* @param logo 右下角的水印图片
*/
+ (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo;
@end
UIImage+MJ.m
#import "UIImage+MJ.h" @implementation UIImage (MJ)
+ (instancetype)waterImageWithBg:(NSString *)bg logo:(NSString *)logo
{
UIImage *bgImage = [UIImage imageNamed:bg]; // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0); // 2.画背景
[bgImage drawInRect:CGRectMake(, , bgImage.size.width, bgImage.size.height)]; // 3.画右下角的水印
UIImage *waterImage = [UIImage imageNamed:logo];
CGFloat scale = 0.2;
CGFloat margin = ;
CGFloat waterW = waterImage.size.width * scale;
CGFloat waterH = waterImage.size.height * scale;
CGFloat waterX = bgImage.size.width - waterW - margin;
CGFloat waterY = bgImage.size.height - waterH - margin;
[waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)]; // 4.从上下文中取得制作完毕的UIImage对象
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 5.结束上下文
UIGraphicsEndImageContext(); return newImage;
}
@end
使用方式
- (void)viewDidLoad
{
[super viewDidLoad]; // 1.返回水印图片
UIImage *newImage = [UIImage waterImageWithBg:@"scene" logo:@"logo"]; // 2.显示图片
self.iconView.image = newImage; // 3.将image对象压缩为PNG格式的二进制数据
NSData *data = UIImagePNGRepresentation(newImage); // 4.写入文件
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
[data writeToFile:path atomically:YES];
}
图片裁剪
UIImage+MJ.h
#import <UIKit/UIKit.h> @interface UIImage (MJ)
+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;
@end
UIImage+MJ.m
#import "UIImage+MJ.h" @implementation UIImage (MJ) + (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
// 1.加载原图
UIImage *oldImage = [UIImage imageNamed:name]; // 2.开启上下文
CGFloat imageW = oldImage.size.width + * borderWidth;
CGFloat imageH = oldImage.size.height + * borderWidth;
CGSize imageSize = CGSizeMake(imageW, imageH);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); // 3.取得当前的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(); // 4.画边框(大圆)
[borderColor set];
CGFloat bigRadius = imageW * 0.5; // 大圆半径
CGFloat centerX = bigRadius; // 圆心
CGFloat centerY = bigRadius;
CGContextAddArc(ctx, centerX, centerY, bigRadius, , M_PI * , );
CGContextFillPath(ctx); // 画圆 // 5.小圆
CGFloat smallRadius = bigRadius - borderWidth;
CGContextAddArc(ctx, centerX, centerY, smallRadius, , M_PI * , );
// 裁剪(后面画的东西才会受裁剪的影响)
CGContextClip(ctx); // 6.画图
[oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)]; // 7.取图
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 8.结束上下文
UIGraphicsEndImageContext(); return newImage;
}
@end
使用方法:
- (void)viewDidLoad
{
[super viewDidLoad]; UIImage *newImage = [UIImage circleImageWithName:@"me" borderWidth: borderColor:[UIColor whiteColor]];
self.iconView.image = newImage; NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
[data writeToFile:path atomically:YES];
}
效果

图片截图
UIImage+MJ.h
#import <UIKit/UIKit.h> @interface UIImage (MJ)
+ (instancetype)captureWithView:(UIView *)view;
@end
UIImage+MJ.m
#import "UIImage+MJ.h" @implementation UIImage (MJ)
+ (instancetype)captureWithView:(UIView *)view
{
// 1.开启上下文
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0); // 2.将控制器view的layer渲染到上下文
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; // 3.取出图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 4.结束上下文
UIGraphicsEndImageContext(); return newImage;
}
@end
使用方法
- (IBAction)clip {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 1.捕捉
UIImage *newImage = [UIImage captureWithView:self.view];
// 2.写文件
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
[data writeToFile:path atomically:YES];
});
}
iOS UI进阶-1.1 Quartz2D 图片水印/裁剪/截图的更多相关文章
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- IOS第17天(1,Quartz2D图片水印)
****图片 水印 #import "HMViewController.h" @interface HMViewController () @property (weak, non ...
- [iOS UI进阶 - 0] Quiartz2D
A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...
- [iOS UI进阶 - 1] 自定义控件
A.关于Quiartz2D的一些细节 1.UIKit的工具已经封装了上下文引用,所以不用手动获取和渲染 - (void)drawRect:(CGRect)rect { [[UIColor redCol ...
- [iOS UI进阶 - 6.1] 核心动画CoreAnimation
A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- [iOS UI进阶 - 2.0] 彩票Demo v1.0
A.需求 1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo 2.v1.0 版本搭建基本框架 code source:https://github.com/hellovoidworld/H ...
- IOS第17天(2,Quartz2D图片剪裁变圆行图,和截屏图片)
**** #import "HMViewController.h" #import "UIImage+Tool.h" @interface HMViewCont ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
随机推荐
- Jexus 安装asp.net mvc EF 项目引发的错误总
1.Linux 中的文件路径问题(配置文件路径),必须使用左斜杆 “/” 2.MVC 看 View/Web.config 下的配置文件中版本不对报错,如下: Could not locate Razo ...
- tomcat启动项目报错:The specified JRE installation does not exist
在Build Path里设置好jre和各Library的顺序,代码无报错,启动时弹框,里面的信息是:The specified JRE installation does not exist. 后来想 ...
- 阿里云不同账号之间相同地域的VPC网络互访
今天实际操作了一下,在这篇随笔中记录一下以备忘,主要参考阿里云帮助文档-不同账号下专有网络内网互通. 实现场景:账号A的VPC网络中的ECS访问账号B的VPC网络中的ECS与RDS(地域都在华东1), ...
- mysql日期格式转换,如何保持原日期?CONVERT/Substring 函数截取。replace替换
http://www.cnblogs.com/stevenjson/p/3729577.html CONVERT(varchar(100), getdate(), 112)这种, 问题就出在getda ...
- [No0000123]WPF DataGrid Columns Visibility的绑定
场景:根据配置文件显示DataGrid中的某些列. 问题:Columns集合只是DataGrid的一个属性,这个集合在逻辑树或视觉树中是看不到的,也不会继承DataContext属性. 方法一:对Da ...
- 两层LSTM的使用
一层的lstm效果不是很好,使用两层的lstm,代码如下. with graph.as_default(): inputs_ = tf.placeholder(tf.int32, [None, seq ...
- tensorflow的tile使用
当你需要按照矩阵维度复制数据时候,可以使用tensorflow的tile函数 a1 = tf.tile(a, [2, 2]) 表示把a的第一个维度复制两次,第二个维度复制2次.注意使用tf.nn.so ...
- MySQL8.0安装连接Navicat的坑
刚在官网装好MySQL8.0后,我的cmd识别不了启动数据库的指令 需要cd到MySQL的bin目录配置mysql mysqld --install mysqld --remove mysql -u ...
- 【Python全栈-后端开发】数据库进阶
数据库进阶 python关于mysql的API---pymysql模块 pymsql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同. 模块安装 pip install ...
- 安装MongoDB报错
尝试多次,最后找到解决方式: 在安装的最后一步的时候不要勾选左下角的compass即可 命令行mongod --version测试安装是否成功