PDF operations in IOS (1)

本文介绍了如何使用Quartz 2D在iPhone OS中创建自定义绘图上下文,并提供了使用CGBitmapContextCreate和CGPDFContextCreate的具体步骤。还强调了在创建位图或PDF上下文时坐标系统的差异及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Refered from article "iPhonedrawingwithquarz2d "

 

Drawing to a Graphics Context in iPhone OS

-------------------------------------------------------

// Obtain this graphics context by calling the UIKit function
CGContextRef context = UIGraphicsGetCurrentContext();
   
// Save graphics contexts using the functions
UIGraphicsPushContext(context);
   
// Restore graphics contexts using the functions
UIGraphicsPopContext();

 

You can create custom graphics context objects in situations where you want to draw somewhere other than your view. For example, you may want to capture a series of drawing commands and use them to create an image or a PDF file. To create the context, you use the

CGBitmapContextCreate or CGPDFContextCreate function. After you have the context, you can pass it to the drawing functions needed to create your content.

 

When creating custom contexts, the coordinate system for those contexts is different from the native coordinate system used by iPhone OS. Instead of the origin being in the upper-left corner of the drawing surface, it is in the lower-left corner and the axes point up and to the right. Instead of the origin being in the upper-left corner of the drawing surface, it is in the lower-left corner and the axes point up and to the right . The coordinates you specify in your drawing commands must take this into consideration or the resulting image or PDF file may appear wrong when rendered. See “Creating a Bitmap Graphics Context ” (page 39) and “Creating a PDF Graphics Context ” (page 35) for details on using CGBitmapContextCreate and CGPDFContextCreate.

 

IMPORTANT : Because you use a lower-left origin when drawing into a bitmap or PDF context,you must compensate for that coordinate system when rendering the resulting content into a view. In other words,

if you create an image and draw it using the CGContextDrawImage function, the image will appear upside down by default . To correct for this, you must invert the y-axis of the CTM (by multiplying it by -1) and shift the origin from the lower-left corner to the top-right corner of the view.
If you use a UIImage object to wrap a CGImageRef you create, you do not need to modify the CTM . The UIImage object automatically compensates for the inverted coordinate system of the CGImageRef type.

 

 

Creating a PDF Graphics Context

-------------------------------------------------------

When you create a PDF graphics context and draw to that context, Quartz records your drawing as a series of PDF drawing commands written to a file. You supply a location for the PDF output and a default media box —a rectangle that specifies bounds of the page.

 

The Quartz 2D API provides two functions that create a PDF graphics context:
■    CGPDFContextCreateWithURL , which you use when you want to specify the location for the PDF output as a Core Foundation URL. Listing 2-4 (page 36) shows how to use this function to create a PDF graphics context.
■    CGPDFContextCreate , which you use when you want the PDF output sent to a data consumer. (For more information see “Data Management in Mac OS X” (page 131).) Listing 2-5 (page 37) shows how to use this function to create a PDF graphics context.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值