封装CoreGraphics的API简化绘图操作

效果

说明

1. 将CoreGraphics的API接口抽象为对象,让绘图变得简单易懂

2. 简化常用的绘制操作

3. 源码长期更新

源码

https://github.com/YouXianMing/CGContextObject

//
// CGContextObject.h
// DrawRect
//
// Created by YouXianMing on 15/7/2.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import "RGBColor.h"
#import "GradientColor.h"
@class CGContextObject; typedef void(^CGContextObjectDrawBlock_t)(CGContextObject *contextObject); @interface CGContextObject : NSObject /**
* 操作句柄
*/
@property (nonatomic) CGContextRef context; /**
* 线头样式
*/
@property (nonatomic) CGLineCap lineCap; /**
* 线条宽度
*/
@property (nonatomic) CGFloat lineWidth; /**
* 线条颜色
*/
@property (nonatomic, strong) RGBColor *strokeColor; /**
* 填充颜色
*/
@property (nonatomic, strong) RGBColor *fillColor; /**
* 由context进行初始化
*
* @param context 绘制句柄
*
* @return 绘制对象
*/
- (instancetype)initWithCGContext:(CGContextRef)context; #pragma mark - 绘制操作流程
/**
* 开始path
*/
- (void)beginPath; /**
* 关闭path
*/
- (void)closePath; /**
* 线条绘制
*/
- (void)strokePath; /**
* 填充绘制
*/
- (void)fillPath; /**
* 线条绘制 + 填充绘制
*/
- (void)strokeAndFillPath; /**
* 绘制线条用block (beginPath + closePath + 你绘制的代码 + strokePath)
*
* @param block 绘制用block
*/
- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block; /**
* 填充区域用block (beginPath + closePath + 你绘制的代码 + fillPath)
*
* @param block 填充用block
*/
- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block; /**
* 绘制加填充
*
* @param block 绘制加填充用block
*/
- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block; /**
* 绘制线条用block (beginPath + closePath + 你绘制的代码 + strokePath)
*
* @param block 绘制用block
* @param closePath 是否关闭曲线
*/
- (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath; /**
* 填充区域用block (beginPath + closePath + 你绘制的代码 + fillPath)
*
* @param block 绘制用block
* @param closePath 是否关闭曲线
*/
- (void)drawFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath; /**
* 绘制加填充
*
* @param block 绘制用block
* @param closePath 是否关闭曲线
*/
- (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath; #pragma mark - 绘制图片API - (void)drawImage:(UIImage *)image atPoint:(CGPoint)point;
- (void)drawImage:(UIImage *)image atPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)drawImage:(UIImage *)image inRect:(CGRect)rect;
- (void)drawImage:(UIImage *)image inRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
- (void)drawImage:(UIImage *)image asPatternInRect:(CGRect)rect; #pragma mark - 保存操作 /**
* 将当前设置存取到栈区中(入栈操作)
*/
- (void)saveStateToStack; /**
* 从栈区中取出之前保存的设置(出栈操作)
*/
- (void)restoreStateFromStack; #pragma mark - 图形绘制API
/**
* 移动到起始点
*
* @param point 起始点
*/
- (void)moveToStartPoint:(CGPoint)point; /**
* 添加一个点(与上一个点直线相连)
*
* @param point 点
*/
- (void)addLineToPoint:(CGPoint)point; /**
* 添加二次贝塞尔曲线
*
* @param point 结束点
* @param pointOne 控制点1
* @param pointTwo 控制点2
*/
- (void)addCurveToPoint:(CGPoint)point controlPointOne:(CGPoint)pointOne controlPointTwo:(CGPoint)pointTwo; /**
* 添加一次贝塞尔曲线
*
* @param point 结束点
* @param controlPoint 控制点
*/
- (void)addQuadCurveToPoint:(CGPoint)point controlPoint:(CGPoint)controlPoint; /**
* 在指定的区域填充彩色的矩形(此为直接绘制)
*
* @param rect 指定的区域
* @param gradientColor 渐变色对象
*/
- (void)drawLinearGradientAtClipToRect:(CGRect)rect gradientColor:(GradientColor *)gradientColor; #pragma mark -
/**
* 添加一个矩形
*
* @param rect
*/
- (void)addRect:(CGRect)rect; /**
* 在给定的矩形中绘制椭圆
*
* @param rect
*/
- (void)addEllipseInRect:(CGRect)rect; /**
* 将string绘制在指定的点上
*
* @param string 字符串
* @param point 点
* @param attributes 富文本设置(可以为空)
*/
- (void)drawString:(NSString *)string atPoint:(CGPoint)point withAttributes:(NSDictionary *)attributes; /**
* 将string绘制在制定的区域
*
* @param string 字符串
* @param rect 区域
* @param attributes 富文本设置(可以为空)
*/
- (void)drawString:(NSString *)string inRect:(CGRect)rect withAttributes:(NSDictionary *)attributes; /**
* 将富文本绘制在制定的点上
*
* @param string 富文本
* @param point 点
*/
- (void)drawAttributedString:(NSAttributedString *)string atPoint:(CGPoint)point; /**
* 将富文本绘制在制定的矩形中
*
* @param string 富文本
* @param rect 矩形
*/
- (void)drawAttributedString:(NSAttributedString *)string inRect:(CGRect)rect; @end
//
// CGContextObject.m
// DrawRect
//
// Created by YouXianMing on 15/7/2.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "CGContextObject.h" @interface CGContextObject () @end @implementation CGContextObject - (instancetype)initWithCGContext:(CGContextRef)context { self = [super init];
if (self) { self.context = context;
} return self;
} - (void)moveToStartPoint:(CGPoint)point { if (_context) {
CGContextMoveToPoint(_context, point.x, point.y);
}
} - (void)addLineToPoint:(CGPoint)point { if (_context) {
CGContextAddLineToPoint(_context, point.x, point.y);
}
} - (void)addCurveToPoint:(CGPoint)point controlPointOne:(CGPoint)pointOne controlPointTwo:(CGPoint)pointTwo { if (_context) {
CGContextAddCurveToPoint(_context, pointOne.x, pointOne.y, pointTwo.x, pointTwo.y, point.x, point.y);
}
} - (void)addQuadCurveToPoint:(CGPoint)point controlPoint:(CGPoint)controlPoint { if (_context) {
CGContextAddQuadCurveToPoint(_context, controlPoint.x, controlPoint.y, point.x, point.y);
}
} - (void)drawLinearGradientAtClipToRect:(CGRect)rect gradientColor:(GradientColor *)gradientColor { [self saveStateToStack]; if (_context) { CGContextClipToRect(_context, rect); CGContextDrawLinearGradient(_context,
gradientColor.gradientRef,
gradientColor.gradientStartPoint,
gradientColor.gradientEndPoint, kCGGradientDrawsBeforeStartLocation);
} [self restoreStateFromStack];
} - (void)addRect:(CGRect)rect { if (_context) {
CGContextAddRect(_context, rect);
}
} - (void)addEllipseInRect:(CGRect)rect { if (_context) {
CGContextAddEllipseInRect(_context, rect);
}
} - (void)drawString:(NSString *)string atPoint:(CGPoint)point withAttributes:(NSDictionary *)attributes { [string drawAtPoint:point withAttributes:attributes];
} - (void)drawString:(NSString *)string inRect:(CGRect)rect withAttributes:(NSDictionary *)attributes { [string drawInRect:rect withAttributes:attributes];
} - (void)drawAttributedString:(NSAttributedString *)string atPoint:(CGPoint)point { [string drawAtPoint:point];
} - (void)drawAttributedString:(NSAttributedString *)string inRect:(CGRect)rect { [string drawInRect:rect];
} - (void)beginPath { if (_context) {
CGContextBeginPath(_context);
}
} - (void)closePath { if (_context) {
CGContextClosePath(_context);
}
} - (void)strokePath { if (_context) {
CGContextStrokePath(_context);
}
} - (void)fillPath { if (_context) {
CGContextFillPath(_context);
}
} - (void)strokeAndFillPath { if (_context) {
CGContextDrawPath(_context, kCGPathFillStroke);
}
} - (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); [self closePath]; [self strokePath];
} - (void)drawFillBlock:(CGContextObjectDrawBlock_t)block { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); [self closePath]; [self fillPath];
} - (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); [self closePath]; [self strokeAndFillPath];
} - (void)drawStrokeBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); if (closePath) {
[self closePath];
} [self strokePath];
} - (void)drawFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); if (closePath) {
[self closePath];
} [self fillPath];
} - (void)drawStrokeAndFillBlock:(CGContextObjectDrawBlock_t)block closePath:(BOOL)closePath { [self beginPath]; __weak CGContextObject *weakSelf = self; block(weakSelf); if (closePath) {
[self closePath];
} [self strokeAndFillPath];
} - (void)drawImage:(UIImage *)image atPoint:(CGPoint)point { [image drawAtPoint:point];
} - (void)drawImage:(UIImage *)image atPoint:(CGPoint)point blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha { [image drawAtPoint:point blendMode:blendMode alpha:alpha];
} - (void)drawImage:(UIImage *)image inRect:(CGRect)rect { [image drawInRect:rect];
} - (void)drawImage:(UIImage *)image inRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha { [image drawInRect:rect blendMode:blendMode alpha:alpha];
} - (void)drawImage:(UIImage *)image asPatternInRect:(CGRect)rect { [image drawAsPatternInRect:rect];
} - (void)saveStateToStack { if (_context) {
CGContextSaveGState(_context);
}
} - (void)restoreStateFromStack { if (_context) {
CGContextRestoreGState(_context);
}
} #pragma mark - 重写setter,getter方法
@synthesize strokeColor = _strokeColor;
- (void)setStrokeColor:(RGBColor *)strokeColor { if (_context) { _strokeColor = strokeColor;
CGContextSetRGBStrokeColor(_context, strokeColor.red, strokeColor.green, strokeColor.blue, strokeColor.alpha);
}
}
- (RGBColor *)strokeColor { return _strokeColor;
} @synthesize fillColor = _fillColor;
- (void)setFillColor:(RGBColor *)fillColor { if (_context) { _fillColor = fillColor;
CGContextSetRGBFillColor(_context, fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha);
}
}
- (RGBColor *)fillColor { return _fillColor;
} @synthesize lineWidth = _lineWidth;
- (void)setLineWidth:(CGFloat)lineWidth { if (_context) { _lineWidth = lineWidth;
CGContextSetLineWidth(_context, lineWidth);
}
}
- (CGFloat)lineWidth { return _lineWidth;
} @synthesize lineCap = _lineCap;
- (void)setLineCap:(CGLineCap)lineCap { if (_context) { _lineCap = lineCap;
CGContextSetLineCap(_context, lineCap);
} }
- (CGLineCap)lineCap { return _lineCap;
} @end

细节

封装CoreGraphics的API简化绘图操作的更多相关文章

  1. javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?

    通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...

  2. 八:SpringBoot-集成JPA持久层框架,简化数据库操作

    SpringBoot-集成JPA持久层框架,简化数据库操作 1.JPA框架简介 1.1 JPA与Hibernate的关系: 2.SpringBoot整合JPA Spring Data JPA概述: S ...

  3. 深入浅出话VC++(3)——VC++实现绘图操作

    VC++实现绘图操作,说白了也就是对API熟练操作了,下面介绍几种绘图 1. 绘制线条 具体实现代码如下: // 鼠标左键按下时的处理函数 void CDrawView::OnLButtonDown( ...

  4. 利用SolrJ操作solr API完成index操作

    使用SolrJ操作Solr会比利用httpClient来操作Solr要简单.SolrJ是封装了httpClient方法,来操作solr的API的.SolrJ底层还是通过使用httpClient中的方法 ...

  5. HBase篇--HBase操作Api和Java操作Hbase相关Api

    一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...

  6. C# 使用 Index 和 Range 简化集合操作

    C# 使用 Index 和 Range 简化集合操作 Intro 有的语言数组的索引值是支持负数的,表示从后向前索引,比如:arr[-1] 从 C# 8 开始,C# 支持了数组的反向 Index,和 ...

  7. Web API与文件操作

    前段时间,一直有练习ASP.NET MVC与Web API交互,接下来,Insus.NET再做一些相关的练习,Web API与文件操作,如POST文件至Web API,更新或是删除等. 不管怎样,先在 ...

  8. Java封装自己的Api

    转自:http://www.2cto.com/kf/201404/291555.html 随着学习的深入,我们都想封装自己的Api,但对于新手这并不是一件简单容易的事! 我要达到的效果:自己封装一些方 ...

  9. Asp.Net Web API 2(CRUD操作)第二课

    Asp.Net Web API 2(CRUD操作)第二课 Asp.Net Web API 导航   Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok ...

随机推荐

  1. elasticsearch(三) 之 elasticsearch目录介绍和配置文件详解

    目录 elasticsearch 配置 目录详情 (config) 配置文件 elasticsearch.yml 配置集群名称(cluster.name) 配置 network.host 更改数据和储 ...

  2. (技术分享) 解决 Firefox 显示“已阻止载入混合活动内容”的问题

    (摘自http://blog.aizhet.com/Windows/18415.html) 从 Firefox 18 开始,如果 HTTPS 页面中包含非加密的 HTTP 内容,浏览器会在控制台输出警 ...

  3. Eclipse内存讲解,eclipse.ini设置

    Ubuntu 系统下,Eclipse 配置文件: vi ~/eclipse/eclipse.ini -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:Max ...

  4. Angular待办事项应用2

    todo组件 接上一篇,在根目录创建todo组件 命令行输入:ng g c todo 得到 文件结构 修改默认路由为todo: 然后打开浏览器:http://localhost:4200/ ,查看,t ...

  5. [转]C# 关闭嵌在程序中的word进程而不关闭用户通过word手动打开的word进程

    命名空间 :System.Diagnostics 以前在word的时候,经常碰到word进程产生一大堆,怕关错了,把用户自己打开的word也关闭,一直搞忽悠,今天上网花了10块钱,下了个文件,给我了一 ...

  6. Linux学习2-Linux分区方式

    1.磁盘分区 磁盘分区是使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分.碟片一旦划分成数个分区(partition),不同类的目录与文件可以存储进不同的分区. 未经过分类整 ...

  7. mysql索引类型normal,unique,full text的区别

    normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...

  8. Exam E05-001 Information Storage and Management Version 3 Exam

    Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...

  9. substr与substring的区别

    在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...

  10. Java设计模式—建造者模式

    建造模式:        将一个复杂的对象的构建与它的表示分离,使得同样的构建 过程可以创建不同的. 建造模式表示是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内 ...