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);

目录
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
8天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
12 0
|
23天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
199 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
28 1
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
93 2