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)