`

PDF operations in IOS (1)

阅读更多

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.

 

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics