ios:quartz2d drawing (displaying a picture drawn on a pdf)

Source: Internet
Author: User

QUART2D can be used to draw images to a PDF or to read and display images from a PDF. Before using this method, there is also a way to read the image displayed on the PDF, that is, using the UIWebView Page view Control-(void) Loadrequest: (Nsurlrequest *) The request method loads the drawing onto the view display, Here I will show both of these ways.

The specific examples are as follows:

Method One: Display the drawing on the PDF in the way that the Page view control UIWebView

1. Drag two child controls in the storyboard view, the Page view Control WebView and the display button

2. Associate the Page view Control WebView Iboutlet to the Controller class to add display events for the display button ibaction

3. Write button event code to display pictures drawn on PDF

#param mark-Display PDF content using the Page view Control

-(Ibaction) Showpdf: (UIButton *) sender{//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"]; //Create URLNsurl *url =[Nsurl Urlwithstring:pdfpath]; //Create requestNsurlrequest *request =[Nsurlrequest Requestwithurl:url]; //Show in Web page[Self.webview loadrequest:request];}

When the button is clicked, the drawing on the displayed PDF is:

Method Two: Use the Quartz2d method to display the drawing on the PDF with its encapsulated function

1. Drag two button controls into the storyboard view, named Previous and next, to look at the front and back drawings.

2. Customize a view class Pdfview and associate the controller view with this class, and drag the pdf file previously saved in the sandbox directory imagebook.pdf into the file

3. Add an event for two buttons in the custom Pdfview class Ibaction

4, and then the specific code, as follows:

In the Pdfview class:

// PDFView.h file to open PDF file method and draw picture Display method

@interface Pdfview:uiview // Open PDF File -(void) Openpdf: (Nsurl *) URL; // The drawing picture is displayed on the View -(void) Drawpdfwithpage: (size_t) page Andcontext: (cgcontextref) context; @end

The pdfview.m file describes some of the PDF properties

@implementation pdfview{    //PDF document     cgpdfdocumentref _pdfdoc;     // Current Page     size_t _currentpagenum;     // Total Pages     size_t _totalpagenums;}

// Open PDF File

-(void) Openpdf: (Nsurl *) url{    // create Pdfdoc    _pdfdoc =  Cgpdfdocumentcreatewithurl ((__bridge cfurlref) URL);         // Total Pages    _totalpagenums = cgpdfdocumentgetnumberofpages (_pdfdoc);         // first page    1 ;}

// Previous Page

-(Ibaction) Pageprev: (UIButton *) sender{    if (_currentpagenum >1)    {        _ Currentpagenum--;        [Self setneedsdisplay];}    }

// Next Page

-(Ibaction) Pagenext: (UIButton *) sender{    if (_currentpagenum < _totalpagenums)    {        _currentpagenum+ +;        [Self setneedsdisplay];}    }

// Show PDF

-(void) Drawpdfwithpage: (size_t) page Andcontext: (cgcontextref) context{    // get page     cgpdfpageref pdfpage = cgpdfdocumentgetpage (_pdfdoc, page);         // drawing a PDF page     cgcontextdrawpdfpage (context, pdfpage);}

// rewrite drawrect: (CGRect Rect) method to draw an image obtained from a PDF file (because the Quartzd coordinate system is reversed, you need to rotate the coordinate system)

-(void) DrawRect: (cgrect) rect{    = uigraphicsgetcurrentcontext ();         // rotate coordinate system    , self.frame.size.height-);     1,-1);        [Self Drawpdfwithpage:_currentpagenum andcontext:context];}

In the Controller Viewcontroller class:

// Call Execution

-(void) viewdidload {    [super Viewdidload];        // loading PDF files    NSString *pdffilename = [[NSBundle mainbundle]pathforresource:@ 'imagebook' ofType: @" PDF " ];         *url = [Nsurl fileurlwithpath:pdffilename];         *pdfview = (pdfview*) Self.view;        [Pdfview Openpdf:url];}

The demo results are as follows:

ios:quartz2d drawing (displaying a picture drawn on a pdf)

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.