ios:quartz2d drawing (drawing a picture on a PDF file)

Source: Internet
Author: User

quartz2d can also draw a picture on a PDF file, which has its own PDF Graphics context context, which can be drawn after the Uigraphicsbeginpdfcontexttofile method starts the context. Finally, remember to use the Uigraphicsendpdfcontext () method to end the context. When you draw a PDF, you can either draw a single-page PDF, or you can draw a multi-page PDF into a book, and It is important to start a new page with Uigraphicsbeginpdfpage () at the beginning of the drawing. The following shows a PDF that draws a single-page PDF and multiple pages.

The specific examples are as follows:

draw a picture on a single-page PDF

#parma mark-Create a single-page PDF

-(void) createpdf{//set the path to a PDF fileNsarray *documents =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); //Document DirectoryNSString *document =[Documents Lastobject]; //NSLog (@ "%@", document); //Stitching PDF PathsNSString *pdfpath = [Document stringbyappendingpathcomponent:@"img.pdf"]; //start the context of the PDFUigraphicsbeginpdfcontexttofile (Pdfpath, CGRectMake (0,0, the,480), nil); //gets the current drawing contextCgcontextref context =Uigraphicsgetcurrentcontext (); //start a new page of PDFUigraphicsbeginpdfpage (); //Draw a circleCgcontextaddellipseinrect (Context, CGRectMake ( -, -, -, -)); [[Uicolor Redcolor]Set];        Cgcontextdrawpath (context, kcgpatheofillstroke); //Draw TextNSString *str =@"This is a image page"; [Str drawatpoint:cgpointmake ( -, -) Withattributes:@{nsforegroundcolorattributename:[uicolor Redcolor],nsfontattributename:[uifont SystemFontOfSize : -]}]; //end the context of the PDFuigraphicsendpdfcontext ();}

Create the Img.pdf file in the sandbox directory and draw the circle graphic and text on the Img.pdf file as:

draw multiple pictures on a multi-page PDF

First, you import more than one smiley face picture, and the imported footage is:

#parma Mark-Create a multi-page PDF

-(void) createpdfbook{//set the path to a PDF fileNsarray *documents =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); //Document DirectoryNSString *document =[Documents Lastobject]; //NSLog (@ "%@", document); //Stitching PDF PathsNSString *pdfpath = [Document stringbyappendingpathcomponent:@"imagebook.pdf"]; //starting PDF Context (parameters: setting PDF path, setting PDF size, accompanying PDF dictionary information)Uigraphicsbeginpdfcontexttofile (Pdfpath, CGRectMake (0,0, the,480), nil); //Drawing Pictures     for(inti =0; i<9; i++)    {        //start a new pageUigraphicsbeginpdfpage (); //Drawing PicturesUIImage *image = [UIImage imagenamed:[nsstring stringWithFormat:@"%d.png", I]]; [Image Drawinrect:cgrectmake ( -, $, -, -)]; }        //end the context of the PDFuigraphicsendpdfcontext ();}

Create the Imgbook.pdf file in the sandbox directory and draw the smiley face on the imgbook.pdf file as:

A total of 9 pictures, so the creation of 9 pages of pdf,9 smiley face pictures were drawn on the above.

ios:quartz2d drawing (drawing a picture on a PDF file)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.