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 ...
随机推荐
- 异常could not retrieve snapshot
前两天项目升级,项目部署到生产上之后,报错: could not retrieve snapshot 上网查的结果是: “.hbm.xml中的字段没有与数据库中一样,就是说有些字段在文件中有,但是在数 ...
- VS没办法调试,直接退出,报错:1. 使用调试生成配置或禁用调试选项“启用‘仅我的代码’”。。。
打开一个Demo,结果没办法调试,运行出错,直接退出了, 明明加了断点的. 输出→调试→提示信息如下 . 使用调试生成配置或禁用调试选项“启用‘仅我的代码’”. . 检查调试选项下的“符号”设置.线程 ...
- hadoop 0.20.2伪分布式安装详解
adoop 0.20.2伪分布式安装详解 hadoop有三种运行模式: 伪分布式不需要安装虚拟机,在同一台机器上同时启动5个进程,模拟分布式. 完全分布式至少有3个节点,其中一个做master,运行名 ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
- [No0000118]SQL Server附加数据库拒绝访问解决方法汇总
修改权限 打开要附加的数据库文件所在的文件夹,右键单击mdf文件,选择“属性”: 单击“安全”选项卡,给所有用户添加读写权限. 最后点击"确定"就可以了.修改权限完成后,你就可以成 ...
- [daily][samba] smbclient使用
用的也不是太明白,反正凑合用吧. 在用之前,只得到了两个信息,1:ip 192.168.30.9. 2:可以免密登录. 1. 用这个命令看一看,主要是找到这个目录:Anonymous ┬─[t ...
- [hardware][intel] intel全系列网卡调研
除了公司用,我自己还要买一块家用. 但是在这一切开始之前,还需要搞清楚PCIE到底咋回事. 一, 总线 https://zh.wikipedia.org/wiki/%E6%80%BB%E7%BA%BF ...
- mongodb操作文件
mongodb操作文件,主要是通过GridFS类.存储文件主要存放在fs中,其中的fs是数据库默认的.并且GridFS是直接与数据库打交道,与collection集合无关. ============= ...
- airflow
基于airflow官方镜像制作自己的镜像,如给镜像安装pymongo FROM /common/air_grpc: MAINTAINER zhangchunyang@goldwind.com.cn U ...
- mysql分库 分表
原文链接:http://www.jianshu.com/p/89311703b320 传统的分库分表传统的分库分表都是通过应用层逻辑实现的,对于数据库层面来说,都是普通的表和库.分库分库的原因 首先, ...