iOS开发CoreGraphics核心图形框架之九——PDF文件的渲染与创建(一)

简介: iOS开发CoreGraphics核心图形框架之九——PDF文件的渲染与创建

一、渲染已有的PDF文档


   在CoreGraphics框架中,有两个类型与PDF文档的渲染有关,分别为CGPDFDocumentRef与CGPDFPageRef。其中,CGPDFDocumentRef对应整个PDF文档,里面封装了许多文档相关的信息,CGPDFPageRef对应PDF文档中某一页的内容,通过它开发者可以将PDF内容通过CGContext上下文渲染到指定目标上。


   如下代码演示了在自定义View的drawRect:方法中进行PDF文档的绘制:


-(void)drawRect:(CGRect)rect{

   //由于坐标系不同,需要进行翻转

   CGContextRef contextRef = UIGraphicsGetCurrentContext();

   //进行坐标系的翻转

   CGContextTranslateCTM(contextRef, 0, rect.size.height);

   CGContextScaleCTM(contextRef, 1.0, -1.0);

   //获取pdf文件的路径

   NSString * path = [[NSBundle mainBundle] pathForResource:@"MyText" ofType:@"pdf"];

   CFStringRef pathString = CFStringCreateWithCString(NULL, [path cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);

   //创建url

   CFURLRef url = CFURLCreateWithFileSystemPath(NULL, pathString, kCFURLPOSIXPathStyle, 0);

   CFRelease(pathString);

   //进行CGPDFDocumentRef引用的创建

   CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);

   CFRelease(url);

   //获取文档的第1页

   CGPDFPageRef page1 = CGPDFDocumentGetPage(document, 1);

   //进行绘制

   CGContextDrawPDFPage(contextRef, page1);

   CGPDFPageRelease(page1);

   CGPDFDocumentRelease(document);

}

效果如下:

image.png



CGPDFDocument中提供的方法解析如下:


//通过数据提供者类来创建PDF文档对象

CGPDFDocumentRef  CGPDFDocumentCreateWithProvider(CGDataProviderRef cg_nullable provider);

//通过url来创建PDF文档

CGPDFDocumentRef  CGPDFDocumentCreateWithURL(CFURLRef cg_nullable url);

//进行引用计数+1

CGPDFDocumentRef  CGPDFDocumentRetain(CGPDFDocumentRef cg_nullable document);

//进行引用计数-1,需要注意,其作用和CFRelease()相似,不同的是如果document为NULL,不是发生crash

void CGPDFDocumentRelease(CGPDFDocumentRef cg_nullable document);

//获取PDF文档的版本

void CGPDFDocumentGetVersion(CGPDFDocumentRef cg_nullable document, int *  majorVersion, int *  minorVersion);

//判断文档是否是加密的

bool CGPDFDocumentIsEncrypted(CGPDFDocumentRef cg_nullable document);

//使用密码对PDF文档进行解密 返回值为1表示解密成功

bool CGPDFDocumentUnlockWithPassword(CGPDFDocumentRef cg_nullable document, const char *  password);

//判断PDF文档是否已经解锁

bool CGPDFDocumentIsUnlocked(CGPDFDocumentRef cg_nullable document);

//获取此PDF文档是否允许绘制

bool CGPDFDocumentAllowsPrinting(CGPDFDocumentRef cg_nullable document);

//获取此文档是否允许拷贝

bool CGPDFDocumentAllowsCopying(CGPDFDocumentRef cg_nullable document);

//获取PDF文档的总页数

size_t CGPDFDocumentGetNumberOfPages(CGPDFDocumentRef cg_nullable document);

//获取文档中某页数据

CGPDFPageRef __nullable CGPDFDocumentGetPage(CGPDFDocumentRef cg_nullable document, size_t pageNumber);

//获取文档的目录信息

CGPDFDictionaryRef __nullable CGPDFDocumentGetCatalog(CGPDFDocumentRef cg_nullable document);

//获取文档详情信息

CGPDFDictionaryRef __nullable CGPDFDocumentGetInfo(CGPDFDocumentRef cg_nullable document);

//获取文档id

CGPDFArrayRef __nullable CGPDFDocumentGetID(CGPDFDocumentRef cg_nullable document);

//获取CGPDFDocument类在CoreGraphics框架中的id

CFTypeID CGPDFDocumentGetTypeID(void);


CGPDFDocument中还有一些已经弃用的方法,这些方法现在封装在CGPDFPage中,弃用的方法如下:


CGRect CGPDFDocumentGetMediaBox(CGPDFDocumentRef cg_nullable document,int page);

CGRect CGPDFDocumentGetCropBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetBleedBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetTrimBox(CGPDFDocumentRef cg_nullable document, int page);

CGRect CGPDFDocumentGetArtBox(CGPDFDocumentRef cg_nullable document, int page);

int CGPDFDocumentGetRotationAngle(CGPDFDocumentRef cg_nullable document, int page);

CGPDFPage中的主要方法列举如下:


//进行引用计数+1

CGPDFPageRef CGPDFPageRetain(CGPDFPageRef cg_nullable page);

//进行引用计数-1

void CGPDFPageRelease(CGPDFPageRef cg_nullable page);

//获取对应的PDF文档对象

CGPDFDocumentRef __nullable CGPDFPageGetDocument(CGPDFPageRef cg_nullable page);

//获取当前页是文档中的第几页

size_t CGPDFPageGetPageNumber(CGPDFPageRef cg_nullable page);

//获取与文档此页相关联的媒体区域

/*

typedef CF_ENUM (int32_t, CGPDFBox) {

 kCGPDFMediaBox = 0,

 kCGPDFCropBox = 1,

 kCGPDFBleedBox = 2,

 kCGPDFTrimBox = 3,

 kCGPDFArtBox = 4

};

*/

CGRect CGPDFPageGetBoxRect(CGPDFPageRef cg_nullable page, CGPDFBox box);

//获取此页的旋转角度

int CGPDFPageGetRotationAngle(CGPDFPageRef cg_nullable page);

//transform变换

CGAffineTransform CGPDFPageGetDrawingTransform(CGPDFPageRef cg_nullable page, CGPDFBox box, CGRect rect, int rotate, bool preserveAspectRatio);


目录
相关文章
|
4天前
|
开发工具 Android开发 iOS开发
探索Android与iOS开发的差异与挑战
【7月更文挑战第11天】在移动应用开发的广阔天地中,Android和iOS两大平台如同双子星座般耀眼,各自拥有独特的开发生态和用户群体。本文将深入分析这两个平台的显著差异,从技术架构到开发工具,再到市场定位,揭示它们之间的异同。通过比较,我们不仅能够更好地理解各自的优势和局限,还能洞察未来移动应用开发的趋势。
|
2天前
|
前端开发 开发工具 Swift
学习iOS开发的准备
准备学习iOS开发?确保有Mac和最新Xcode,先学好编程基础特别是Swift。利用Apple官方文档、在线课程和书籍作为资源。熟悉Xcode及Git,通过实践项目和开源代码积累经验。深研架构模式、核心框架和优化技巧。加入开发者社区,关注行业动态,持续学习。
14 1
|
4天前
|
Java 开发工具 Android开发
探索Android与iOS开发的差异与挑战
【7月更文挑战第11天】在移动应用开发的广阔天地中,Android和iOS两大平台各领风骚。本文将深入探讨这两个平台的开发差异,从编程语言、用户界面设计到开发工具等方面进行比较,并分析开发者面临的挑战。通过对比分析,旨在为开发者提供一个全面的视角,帮助他们更好地选择适合自己项目需求的开发平台。
8 0
|
4天前
|
iOS开发 开发者 UED
探索iOS开发中的SwiftUI框架
【7月更文挑战第11天】本文将深入探讨SwiftUI框架,这是苹果公司为iOS开发者提供的一个现代化、声明式的用户界面工具。我们将了解SwiftUI如何简化界面设计流程,提升代码的可读性和可维护性,并讨论其在实际应用中的优势和局限性。通过实际案例分析,本文旨在帮助读者更好地理解SwiftUI的核心概念和应用实践。
|
5天前
|
机器学习/深度学习 人工智能 iOS开发
探索iOS开发的未来:SwiftUI的革新之旅
在数字时代的浪潮中,iOS开发正经历一场由SwiftUI引领的变革。本文将深入探讨SwiftUI如何重塑移动应用开发,提升开发者的生产力,并展望其对iOS生态的影响。通过分析SwiftUI的核心特性、实际案例和未来趋势,我们将揭示这一现代框架如何定义用户体验的新标准,同时为iOS开发者指明技术进阶之路。
|
存储 iOS开发
iOS开发之EventKit框架的应用(一)
iOS开发之EventKit框架的应用
697 0
iOS开发之EventKit框架的应用(一)
|
开发者 iOS开发
iOS开发之DeviceCheck框架的应用
iOS开发之DeviceCheck框架的应用
569 0
iOS开发之DeviceCheck框架的应用
|
区块链 iOS开发 开发者
iOS开发之CoreSpotlight框架的应用
iOS开发之CoreSpotlight框架的应用
257 0
iOS开发之CoreSpotlight框架的应用
|
传感器 iOS开发 开发者
iOS开发之CoreMotion框架的应用
iOS开发之CoreMotion框架的应用
453 0
iOS开发之CoreMotion框架的应用
|
Swift C语言 iOS开发
iOS开发CoreGraphics核心图形框架之一——CGPath的应用(一)
iOS开发CoreGraphics核心图形框架之一——CGPath的应用
299 0
iOS开发CoreGraphics核心图形框架之一——CGPath的应用(一)