Reference Blog
Draw to PDF to enable the PDF graphics context, the PDF graphics context is created in a way that is similar to the bitmap graphics context, and one thing to note is that you need to create a paging when you draw the content to the PDF, and the Uigraphicsbeginpdfpage method is called at the beginning of each page of content. The following example shows text drawing and picture drawing (other drawing is similar):
- (void) viewdidload{[Super Viewdidload]; //Sandbox pathNsarray *patharray =nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES); NSString*path=[[patharray Firstobject] stringByAppendingPathComponent:@"mypdf.pdf"]; //Path : Save path//bounds:pdf The document size, use the default value if set to Cgrectzero: 612*792//pageInfo: Page Setup, nil does not set any informationUigraphicsbeginpdfcontexttofile (Path, Cgrectzero, @{kcgpdfcontextauthor:@"zlt"}); //can also be stored in the Nsmutabledata//Uigraphicsbeginpdfcontexttodata (< #NSMutableData *data#>, < #CGRect Bounds#>, < #NSDictionary * documentinfo#>); //Start the first page drawingUigraphicsbeginpdfpage (); NSString*title =@"Welcome to Apple support"; //Paragraph styleNsmutableparagraphstyle *style =[[Nsmutableparagraphstyle alloc] init]; //AlignmentNstextalignment aligment =Nstextalignmentcenter; Style.alignment=aligment; [Title Drawinrect:cgrectmake ( -, -, -, -) Withattributes:@{nsfontattributename:[uifont Systemfontofsize: -],nsparagraphstyleattributename:style}]; NSString*content =@"Learn about Apple products, view online manuals, get the latest downloads, and more. Connect with other Apple users, or get service, support, and professional advice from Apple."; Nsmutableparagraphstyle*style2 =[[Nsmutableparagraphstyle alloc] init]; Style2.alignment=Nstextalignmentleft; [Content Drawinrect:cgrectmake ( -, About, -,255) Withattributes:@{nsfontattributename:[uifont Systemfontofsize: the],nsforegroundcolorattributename:[uicolor Graycolor],nsparagraphstyleattributename:style2}]; UIImage*image = [UIImage imagenamed:@"Applecare_folks_tall.png"]; [Image Drawinrect:cgrectmake ( the, -,290,305)]; UIImage*image2 = [UIImage imagenamed:@"Applecare_page1.png"]; [Image2 Drawinrect:cgrectmake (6, the, -,281)]; //Create a new page to continue drawing other contentUigraphicsbeginpdfpage (); UIImage*image3 = [UIImage imagenamed:@"Applecare_page2.png"]; [Image3 Drawinrect:cgrectmake (6, -, -,629)]; //End PDF Contextuigraphicsendpdfcontext ();}
Generated PDF document:
Knowledge Supplement
1.Core Graphics is a set of frameworks based on the C language, which cannot be invoked as obj-c.
2. In quartz 2D, any object created with the "create" or "Copy" keyword method must be disposed of using the corresponding method after use (since the framework does not automatically release memory based on the C language);
3.Quartz 2D is a cross-platform, so it is not possible to use the objects in Uikit (Uikit only available for iOS), such as the use of colors can only be used cgcolorref and not with the Uicolor, but Uikit provides the corresponding conversion method;
4. Enumerations in C are generally prefaced with "K", since Quartz 2D is based on C language development, so it is no exception (many of the enumerations in the parameters are K-prefaced);
5. Since quartz 2D is part of the core graphics, most APIs start with CG;
6. All objects ending with "Ref" in the Quartz 2D drawing API are not declared as pointer types at the time of Declaration;
7. When using the Quartz 2D drawing API, all relevant drawing functions at the beginning of the "UI" are uikit to the core graphics (primarily to simplify drawing operations);
iOS image processing (8) drawing in PDF