Skip to content
This repository was archived by the owner on Aug 10, 2020. It is now read-only.

Draw UIView hierarchy manually, for crisp rendering of PDFs from views in iOS

License

Notifications You must be signed in to change notification settings

mruegenberg/uiview-hierarchical-drawing

Repository files navigation

uiview-hierarchical-drawing

This is a category on UIView, which directly calls drawRect hierarchically. This allows you to use any UIGraphicsContext, and in particular PDF contexts. The benefit: Beautiful, crystal clear rendering.

Usage

#import "UIView+HierarchicalDrawing.h"

CGSize pageSize = CGSizeMake(842.4,597.6);
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, pageSize.width, pageSize.height), documentInfoDict);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(pdfContext, kCGInterpolationHigh);

[theView drawHierarchy];

UIGraphicsEndPDFContext();
// use pdfData

Example

Below are two examples of rendering into a PDF context. One uses UIView+HierarchicalDrawing with drawHierarchy, the other -[CALayer renderInContext:] with a contentScaleFactor of 2 on the view.

  • Using UIView+HierarchicalDrawing: hierarchical

  • renderInContext: renderInContext

Known issues

There are some possible problems with custom patterns as view backgrounds.

License

MIT. See LICENSE.

About

Draw UIView hierarchy manually, for crisp rendering of PDFs from views in iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published