图形框架: |
CoreGraphics.framework |
包含 Quartz 2D 绘图 API 接口 。Quartz 是 Mac OS X 系统使用的向量绘图引擎,它支持基于路径绘图、抗锯齿渲染、渐变、图片、颜色、坐标空间转换、PDF 文件的创建、显示和解析。虽然 API 基于 C 语言,但是它使用基于对象的抽象以表示基本绘图对象,这样可以让开发者可以更方便地保存并复用图像内容。 |
一.PDF的显示
#import<QuartzCore/QuartzCore.h>准备工作完成
先建一个基于UIView的PDFView
在PDFView.h文件中加载
#import <UIKit/UIKit.h>
@interface PDFView : UIView
{
CGPDFDocumentRef pdf;
}
-(void)drawInContext:(CGContextRef)context;
@end
在PDFView.m文件中加载
#import "PDFView.h"
@implementation PDFView
- (void)dealloc
{
CGPDFDocumentRelease(pdf);
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
if(self != nil)
{
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("苹果编码规范.PDF"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
}
}
return self;
}
-(void)drawInContext:(CGContextRef)context
{
// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
// before we start drawing.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Grab the first PDF page
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
CGContextSaveGState(context);
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
// base rotations necessary to display the PDF page correctly.
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
// Finally, we draw the page and restore the graphics state for further manipulations!
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}
- (void)drawRect:(CGRect)rect {
[self drawInContext:UIGraphicsGetCurrentContext()];
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 320, 480);
PDFView *pdfView =[[PDFView alloc]initWithFrame:frame];
[self.view addSubview:pdfView];
[pdfView release];
}
二.pdf的创建
pdf文档在iOS中是通过Quartz 2D库提供的api来操作的。iOS有两个图形库:
- Quartz 2D,是iOS原生的,简单易用,缺点是只有2D,仅限于iOS
- OpenGL ES,是开放标准的,2D和3D均有
使用Quartz 2D绘图到任意图形上下文(graphics context)创建pdf文件是很容易的事情。这需要:
- 指定pdf文件的位置
- 设置pdf的图形上下文(graphics context)
写了个特别简单的示例,效果如下:
完整的代码如下:
-(void)createPdf{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CGRect pageRect=CGRectMake(0, 0, 612, 792);
// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for ‘signing’ the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it’s time to draw to it
// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);
// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width – 100, pageRect.size.height – 100));
// Adding some text on top of the image we just added
CGContextSelectFont (pdfContext, "Helvetica", 30, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *text = (char *)[@"Hello world" UTF8String];
CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));
// End text
// We are done drawing to this page, let’s end it
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
CGContextEndPage (pdfContext);
// We are done with our context now, so we release it
CGContextRelease (pdfContext);
}
会在当前应用的Documents目录下创建一个myPDF.pdf文件,文件中只有一行字,Hello world。