iOS开发CoreGraphics核心图形框架之二——深入理解图形上下文(二)

简介: iOS开发CoreGraphics核心图形框架之二——深入理解图形上下文

上面有提到,在创建PDF图形上下文时,可以设置一个信息字典,这个字典中常用的可以进行配置的键值如下:


//这个键是可选的 对应需要设置为字符串类型的值 表明文档作者

kCGPDFContextAuthor

//这个键是可选的 对应需要设置为字符串类型的值 表示生成文档的命名名称

kCGPDFContextCreator

//这个键是可选的 对应需要设置为字符串类型的值 表示文档名称

kCGPDFContextTitle

//这个键设置所有者密码 需要设置为CFString的值

kCGPDFContextOwnerPassword

//这个键设置用户密码 需要设置为CFString的值

kCGPDFContextUserPassword

//这个键设置是否允许在未解锁状态下进行打印 需要设置为CFBollean的值 默认为允许

kCGPDFContextAllowsPrinting

//这个键设置是否允许在未解锁状态下进行复制 需要设置为CFBollean的值 默认为允许

kCGPDFContextAllowsCopying

//设置输出规范

kCGPDFContextOutputIntent

kCGPDFContextOutputIntents

//设置文档的主题 需要设置为CFString的值

kCGPDFContextSubject

//设置文档的关键字

kCGPDFContextKeywords

//设置密钥长度

kCGPDFContextEncryptionKeyLength

四、CGContext功能解析


   前边介绍了如何拿到对应的图形上下文,拿到图形上下文后,开发者便可以随心所欲的通过图形上下文向目标上绘制内容。CoreGraphics框架中提供的CGContext绘制相关方法解析如下:


//获取CGContext类在CoreGraphics框架中的id值

CFTypeID CGContextGetTypeID(void);

//将当前图形上下文进行保存 会执行push入栈

void CGContextSaveGState(CGContextRef cg_nullable c);

//将图形上下文恢复到保存时的状态

void CGContextRestoreGState(CGContextRef cg_nullable c);

//对context内容进行缩放操作

void CGContextScaleCTM(CGContextRef cg_nullable c, CGFloat sx, CGFloat sy);

//对context内容进行平移操作

void CGContextTranslateCTM(CGContextRef cg_nullable c, CGFloat tx, CGFloat ty);

//对context内容进行旋转操作

void CGContextRotateCTM(CGContextRef cg_nullable c, CGFloat angle);

//对context内容进行transform变换操作

void CGContextConcatCTM(CGContextRef cg_nullable c,CGAffineTransform transform);

//获取某个context的transform变换对象

CGAffineTransform CGContextGetCTM(CGContextRef cg_nullable c);

//设置绘制的线宽

void CGContextSetLineWidth(CGContextRef cg_nullable c, CGFloat width);

//设置绘制的线帽风格

void CGContextSetLineCap(CGContextRef cg_nullable c, CGLineCap cap);

//设置绘制的线连接处风格

void CGContextSetLineJoin(CGContextRef cg_nullable c, CGLineJoin join);

//设置绘制的先转折处风格

void CGContextSetMiterLimit(CGContextRef cg_nullable c, CGFloat limit);

//设置虚线配置参数

void CGContextSetLineDash(CGContextRef cg_nullable c, CGFloat phase, const CGFloat * __nullable lengths, size_t count);

//设置平滑度

void CGContextSetFlatness(CGContextRef cg_nullable c, CGFloat flatness);

//设置透明度

void CGContextSetAlpha(CGContextRef cg_nullable c, CGFloat alpha);

//设置图像复合模式

void CGContextSetBlendMode(CGContextRef cg_nullable c, CGBlendMode mode);

//开始新的路径 旧的路径将被抛弃

void CGContextBeginPath(CGContextRef cg_nullable c);

//将路径起点移动到某个点

void CGContextMoveToPoint(CGContextRef cg_nullable c, CGFloat x, CGFloat y);

//向路径中添加一条线

void CGContextAddLineToPoint(CGContextRef cg_nullable c,CGFloat x, CGFloat y);

//向路径中添加三次贝塞尔曲线

void CGContextAddCurveToPoint(CGContextRef cg_nullable c, CGFloat cp1x, CGFloat cp1y, CGFloat cp2x, CGFloat cp2y, CGFloat x, CGFloat y);

//向路径中添加二次贝塞尔曲线

void CGContextAddQuadCurveToPoint(CGContextRef cg_nullable c, CGFloat cpx, CGFloat cpy, CGFloat x, CGFloat y);

//闭合路径

void CGContextClosePath(CGContextRef cg_nullable c);

//向路径中添加一个矩形

void CGContextAddRect(CGContextRef cg_nullable c, CGRect rect);

//向路径中添加一组矩形

void CGContextAddRects(CGContextRef cg_nullable c, const CGRect * __nullable rects, size_t count);

//向路径中添加一组线条

void CGContextAddLines(CGContextRef cg_nullable c, const CGPoint * __nullable points, size_t count);

//向路径中添加椭圆

CGContextAddEllipseInRect(CGContextRef cg_nullable c, CGRect rect);

//向路径中添加圆弧

void CGContextAddArc(CGContextRef cg_nullable c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise);

void CGContextAddArcToPoint(CGContextRef cg_nullable c, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius);

//直接向上下文中添加一个路径对象

void CGContextAddPath(CGContextRef cg_nullable c, CGPathRef cg_nullable path);

//将上下文中的路径内容替换掉 只留下边框

void CGContextReplacePathWithStrokedPath(CGContextRef cg_nullable c);

//判断某个Context的路径是否为空

bool CGContextIsPathEmpty(CGContextRef cg_nullable c);

//获取一个Context路径当前端点的位置

CGPoint CGContextGetPathCurrentPoint(CGContextRef cg_nullable c);

//获取路径的尺寸

CGRect CGContextGetPathBoundingBox(CGContextRef cg_nullable c);

//进行图形上下文的拷贝

CGPathRef __nullable CGContextCopyPath(CGContextRef cg_nullable c);

//获取context的路径中是否包含某个点

bool CGContextPathContainsPoint(CGContextRef cg_nullable c, CGPoint point, CGPathDrawingMode mode);

//进行路径的绘制

/*

mode枚举意义如下:

 kCGPathFill,    //进行填充

 kCGPathEOFill,   //补集进行填充绘制

 kCGPathStroke,  //边框绘制

 kCGPathFillStroke, //边框绘制并填充

 kCGPathEOFillStroke //补集进行边框和填充绘制

*/

void CGContextDrawPath(CGContextRef cg_nullable c, CGPathDrawingMode mode);

//进行路径的填充

void CGContextFillPath(CGContextRef cg_nullable c);

//进行路径所围成区域的补集区域填充

void CGContextEOFillPath(CGContextRef cg_nullable c);

//进行边框绘制

void CGContextStrokePath(CGContextRef cg_nullable c);

//填充某个矩形区域

void CGContextFillRect(CGContextRef cg_nullable c, CGRect rect);

//填充一组矩形区域

void CGContextFillRects(CGContextRef cg_nullable c, const CGRect * __nullable rects, size_t count);

//进行矩形区域的边框绘制

void CGContextStrokeRect(CGContextRef cg_nullable c, CGRect rect);

//进行矩形区域的边框绘制 可以设置边框宽度

void CGContextStrokeRectWithWidth(CGContextRef cg_nullable c, CGRect rect, CGFloat width);

//清除某个矩形区域

void CGContextClearRect(CGContextRef cg_nullable c, CGRect rect);

//进行虚线区域的填充

void CGContextFillEllipseInRect(CGContextRef cg_nullable c, CGRect rect);

//进行虚线区域边框的绘制

void CGContextStrokeEllipseInRect(CGContextRef cg_nullable c,CGRect rect);

//绘制一组线

void CGContextStrokeLineSegments(CGContextRef cg_nullable c, const CGPoint * __nullable points, size_t count);

//依据Context当前路径进行裁剪

void CGContextClip(CGContextRef cg_nullable c);

//进行路径区域的补集区域裁剪

void CGContextEOClip(CGContextRef cg_nullable c);

//这个方法十分重要 其可以将图片裁剪成图形上下文定义的形状

void CGContextClipToMask(CGContextRef cg_nullable c, CGRect rect, CGImageRef cg_nullable mask);

//获取裁剪的区域尺寸

CGRect CGContextGetClipBoundingBox(CGContextRef cg_nullable c);

//进行区域裁剪

void CGContextClipToRect(CGContextRef cg_nullable c, CGRect rect);

//进行一组区域的裁剪

void CGContextClipToRects(CGContextRef cg_nullable c, const CGRect *  rects, size_t count);

//设置图形上下文的填充颜色

void CGContextSetFillColorWithColor(CGContextRef cg_nullable c, CGColorRef cg_nullable color);

//设置图形上下文的边框颜色

void CGContextSetStrokeColorWithColor(CGContextRef cg_nullable c, CGColorRef cg_nullable color);

//设置图形上下文填充颜色的色彩空间

void CGContextSetFillColorSpace(CGContextRef cg_nullable c,CGColorSpaceRef cg_nullable space);

//设置图形上下文边框颜色的色彩空间

void CGContextSetStrokeColorSpace(CGContextRef cg_nullable c, CGColorSpaceRef cg_nullable space);

//下面这些函数与设置颜色和组件模块属性相关

void CGContextSetFillColor(CGContextRef cg_nullable c, const CGFloat * cg_nullable components);

void CGContextSetStrokeColor(CGContextRef cg_nullable c, const CGFloat * cg_nullable components);

void CGContextSetFillPattern(CGContextRef cg_nullable c, CGPatternRef cg_nullable pattern, const CGFloat * cg_nullable components);

void CGContextSetStrokePattern(CGContextRef cg_nullable c, CGPatternRef cg_nullable pattern, const CGFloat * cg_nullable components);

void CGContextSetPatternPhase(CGContextRef cg_nullable c, CGSize phase);

void CGContextSetGrayFillColor(CGContextRef cg_nullable c, CGFloat gray, CGFloat alpha);

void CGContextSetGrayStrokeColor(CGContextRef cg_nullable c, CGFloat gray, CGFloat alpha);

void CGContextSetRGBFillColor(CGContextRef cg_nullable c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha);

void CGContextSetRGBStrokeColor(CGContextRef cg_nullable c, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha);

void CGContextSetCMYKFillColor(CGContextRef cg_nullable c, CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha);

void CGContextSetCMYKStrokeColor(CGContextRef cg_nullable c, CGFloat cyan, CGFloat magenta, CGFloat yellow, CGFloat black, CGFloat alpha);

//将当前上下文内容渲染进颜色intent中

void CGContextSetRenderingIntent(CGContextRef cg_nullable c, CGColorRenderingIntent intent);

//在指定区域内渲染图片

void CGContextDrawImage(CGContextRef cg_nullable c, CGRect rect, CGImageRef cg_nullable image);

//在区域内进行瓦片方式的图片渲染

void CGContextDrawTiledImage(CGContextRef cg_nullable c, CGRect rect, CGImageRef cg_nullable image);

//获取上下文渲染的图像质量

CGInterpolationQuality CGContextGetInterpolationQuality(CGContextRef cg_nullable c);

//设置上下文渲染时的图像质量

void CGContextSetInterpolationQuality(CGContextRef cg_nullable c, CGInterpolationQuality quality);

//设置进行阴影的渲染

void CGContextSetShadowWithColor(CGContextRef cg_nullable c,  CGSize offset, CGFloat blur, CGColorRef __nullable color);

void CGContextSetShadow(CGContextRef cg_nullable c, CGSize offset, CGFloat blur);

//绘制线性渐变效果

void CGContextDrawLinearGradient(CGContextRef cg_nullable c,  CGGradientRef cg_nullable gradient, CGPoint startPoint, CGPoint endPoint, CGGradientDrawingOptions options);

//绘制半径渐变效果

void CGContextDrawRadialGradient(CGContextRef cg_nullable c, CGGradientRef cg_nullable gradient, CGPoint startCenter, CGFloat startRadius, CGPoint endCenter, CGFloat endRadius, CGGradientDrawingOptions options);

//用渐变填充上下文的裁剪区域

void CGContextDrawShading(CGContextRef cg_nullable c,  cg_nullable CGShadingRef shading);

//设置绘制的文字间距

void CGContextSetCharacterSpacing(CGContextRef cg_nullable c, CGFloat spacing);

//设置绘制的文字位置

void CGContextSetTextPosition(CGContextRef cg_nullable c, CGFloat x, CGFloat y);

//获取绘制的文字位置

CGPoint CGContextGetTextPosition(CGContextRef cg_nullable c);

//设置文字transform变换

void CGContextSetTextMatrix(CGContextRef cg_nullable c, CGAffineTransform t);

//获取文字的transform变换

CGAffineTransform CGContextGetTextMatrix(CGContextRef cg_nullable c);

//设置文字的绘制模式

/*

 kCGTextFill,           //填充

 kCGTextStroke,         //空心

 kCGTextFillStroke,     //填充加边框

 kCGTextInvisible,      //在可是区域内

 kCGTextFillClip,       //裁剪填充

 kCGTextStrokeClip,     //裁剪绘制边框

 kCGTextFillStrokeClip,//进行裁剪

 kCGTextClip

*/

void CGContextSetTextDrawingMode(CGContextRef cg_nullable c, CGTextDrawingMode mode);

//设置绘制文字的字体

void CGContextSetFont(CGContextRef cg_nullable c, CGFontRef cg_nullable font);

//设置绘制文字的字号

void CGContextSetFontSize(CGContextRef cg_nullable c, CGFloat size);

void CGContextShowGlyphsAtPositions(CGContextRef cg_nullable c, const CGGlyph * cg_nullable glyphs, const CGPoint * cg_nullable Lpositions,  size_t count);

//设置绘制的字符风格

void CGContextShowGlyphsAtPositions(CGContextRef cg_nullable c, const CGGlyph * cg_nullable glyphs, const CGPoint * cg_nullable Lpositions, size_t count);

//进行PDF页的绘制

void CGContextDrawPDFPage(CGContextRef cg_nullable c, CGPDFPageRef cg_nullable page);

//开启一个新的PDF页

void CGContextBeginPage(CGContextRef cg_nullable c,  const CGRect * __nullable mediaBox);

//结束当前的PDF页

void CGContextEndPage(CGContextRef cg_nullable c);

//内存引用加1

CGContextRef cg_nullable CGContextRetain(CGContextRef cg_nullable c);

//内存引用计数减1

void CGContextRelease(CGContextRef cg_nullable c);

//将上下文中的内容立即渲染到目标

void CGContextFlush(CGContextRef cg_nullable c);

//将上下文中的内容进行同步

void CGContextSynchronize(CGContextRef cg_nullable c);

//是否开启抗锯齿效果

void CGContextSetShouldAntialias(CGContextRef cg_nullable c,  bool shouldAntialias);

//是否允许抗锯齿效果

void CGContextSetAllowsAntialiasing(CGContextRef cg_nullable c,  bool allowsAntialiasing);

//是否开启字体平滑

void CGContextSetShouldSmoothFonts(CGContextRef cg_nullable c, bool shouldSmoothFonts);

//是否允许字体平滑

void CGContextSetAllowsFontSmoothing(CGContextRef cg_nullable c, bool allowsFontSmoothing);

//设置是否开启subpixel状态渲染符号

void CGContextSetShouldSubpixelPositionFonts(CGContextRef cg_nullable c, bool shouldSubpixelPositionFonts);

//是否允许subpixel状态渲染符号

void CGContextSetAllowsFontSubpixelPositioning(CGContextRef cg_nullable c, bool allowsFontSubpixelPositioning);

//这个方法会在当前Context中开启一个透明的层 之后的绘制会绘制到这个透明的层上

void CGContextBeginTransparencyLayer(CGContextRef cg_nullable c, CFDictionaryRef __nullable auxiliaryInfo);

//在Context中开启一个透明的层

void CGContextBeginTransparencyLayerWithRect(CGContextRef cg_nullable c, CGRect rect, CFDictionaryRef __nullable auxInfo);

//完成透明层的渲染

void CGContextEndTransparencyLayer(CGContextRef cg_nullable c);

//返回用户控件的transform变换

CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform(CGContextRef cg_nullable c);

//将用户控件点的坐标转换为设备控件坐标

CGPoint CGContextConvertPointToDeviceSpace(CGContextRef cg_nullable c, CGPoint point);

//将设备空间的点坐标转换为用户空间的点坐标

CGPoint CGContextConvertPointToUserSpace(CGContextRef cg_nullable c, CGPoint point);

//将用于空间的尺寸转换为设备空间的尺寸

CGSize CGContextConvertSizeToDeviceSpace(CGContextRef cg_nullable c, CGSize size);

//将设备空间的尺寸转换为用户空间的尺寸

CGSize CGContextConvertSizeToUserSpace(CGContextRef cg_nullable c, CGSize size);

//将用户空间的rect转换为设备空间的rect

CGRect CGContextConvertRectToDeviceSpace(CGContextRef cg_nullable c, CGRect rect);

//将设备空间的rect转换为用户空间的rect

CGRect CGContextConvertRectToUserSpace(CGContextRef cg_nullable c, CGRect rect);


//下面这些方法已经被弃用

//设置字体 使用CoreText相关的API代替

void CGContextSelectFont(CGContextRef cg_nullable c, const char * cg_nullable name, CGFloat size, CGTextEncoding textEncoding);

//绘制文本 使用CoreText相关API代替

void CGContextShowText(CGContextRef cg_nullable c, const char * cg_nullable string, size_t length);

//在相应位置绘制文本 使用CoreText相关API代替

void CGContextShowTextAtPoint(CGContextRef cg_nullable c, CGFloat x, CGFloat y, const char * cg_nullable string, size_t length);

//进行符号的绘制 使用CoreText相关API代替

void CGContextShowGlyphs(CGContextRef cg_nullable c, const CGGlyph * __nullable g, size_t count);

//在相应位置绘制符号 使用CoreText相关API代替

void CGContextShowGlyphsAtPoint(CGContextRef cg_nullable c, CGFloat x, CGFloat y, const CGGlyph * __nullable glyphs, size_t count);

//绘制符号 使用一个固定的缩进值 使用CoreText相关API代替

void CGContextShowGlyphsWithAdvances(CGContextRef cg_nullable c, const CGGlyph * __nullable glyphs, const CGSize * __nullable advances, size_t count);

//进行PDF文档绘制 CGPDFPage相关API代替

void CGContextDrawPDFDocument(CGContextRef cg_nullable c, CGRect rect, CGPDFDocumentRef cg_nullable document, int page);

目录
相关文章
|
移动开发 网络协议 小程序
基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v10.0版已发布
RainbowChat是一套基于开源IM即时通讯聊天框架 MobileIMSDK 的产品级移动端IM系统。RainbowChat源于真实运营的产品,解决了大量的屏幕适配、细节优化、机器兼容问题。RainbowChat可能是市面上提供im即时通讯聊天源码的,唯一一款同时支持TCP、UDP两种通信协议的IM产品。与姊妹产品RainbowTalk和RainbowChat-Web 技术同源,历经考验。
1170 0
基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v10.0版已发布
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
1232 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
675 66
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
826 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
2191 11
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
569 3
|
人工智能 程序员 API
iOS|记一名 iOS 开发新手的前两次 App 审核经历
啥,这玩意也有新手保护期?
658 0
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
609 9
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。

热门文章

最新文章