The simple realization of browsing and graffiti effect of PDF files under IOS _ios

Source: Internet
Author: User

The effect of browsing a PDF
method One: Using WebView

Copy Code code as follows:

-(void) Loaddocument: (NSString *) DocumentName InView: (UIWebView *) WebView
{
NSString *path = [[NSBundle mainbundle] Pathforresource:documentname Oftype:nil];
Nsurl *url = [Nsurl Fileurlwithpath:path];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
[WebView Loadrequest:request];
}


Li: 1. Simple to achieve
2. or simple to achieve
Disadvantages: 1. Can only browse, get no callback, Safari will not bird anyone.
2. Fixed the vertical version of the drag, want to achieve the effect of turning the page blind

The following method can solve the disadvantage of WebView display PDF, relative, to pay some sweat as a price.

Method II: Using Cgcontextdrawpdfpage

Copy Code code as follows:

Cgpdfdocumentref getpdfdocumentref (NSString *filename)
{
Cfstringref path;
Cfurlref URL;
Cgpdfdocumentref document;
size_t count;

Path = cfstringcreatewithcstring (NULL, [filename utf8string], kCFStringEncodingUTF8);
url = Cfurlcreatewithfilesystempath (NULL, Path, Kcfurlposixpathstyle, 0);

Cfrelease (path);
Document = Cgpdfdocumentcreatewithurl (URL);
Cfrelease (URL);
Count = cgpdfdocumentgetnumberofpages (document);
if (count = = 0) {
printf ("[%s] needs at least one page!\n", [filename utf8string]);
return NULL;
} else {
printf ("[%LD] pages loaded in this pdf!\n", count);
}
return document;
}

void Displaypdfpage (Cgcontextref mycontext, size_t pagenumber, NSString *filename)
{
Cgpdfdocumentref document;
Cgpdfpageref page;

Document = Getpdfdocumentref (filename);
page = Cgpdfdocumentgetpage (document, pagenumber);
Cgcontextdrawpdfpage (mycontext, page);
Cgpdfdocumentrelease (document);
}


This display of the PDF single page is inverted, quartz coordinate system and UIView coordinate system, adjust the coordinate system, so that PDF is standing:

Copy Code code as follows:

Cgcontextref context = Uigraphicsgetcurrentcontext ();
CGCONTEXTTRANSLATECTM (context, self.frame.size.height-60);
CGCONTEXTSCALECTM (context, 1,-1);


With iOS5 powerful Uipageviewcontroller to realize paging browsing

Copy Code code as follows:

-(Pdfviewcontroller *) Viewcontrolleratindex: (nsuinteger) index
{
Return to the Pdfviewcontroller for the given index.
if ([self.pagepdf count] = 0) | | (Index > [self.pagepdf Count]) {
return nil;
}

Create a new view controller and pass suitable data.
Pdfviewcontroller *dataviewcontroller = [[Pdfviewcontroller alloc]initwithnibname:@ "PDFViewController" bundle:nil];
Dataviewcontroller.pdfview = [Self.pagepdf objectatindex:index];
Dataviewcontroller.pdfview = [[Pdfview alloc]initwithframe:self.view.frame Atpage:index];
[Dataviewcontroller.view AddSubview:dataViewController.pdfview];
NSLog (@ "index =%d", index);
return dataviewcontroller;
}

-(Nsuinteger) Indexofviewcontroller: (Pdfviewcontroller *) Viewcontroller
{
return [Self.pagepdf IndexOfObject:viewController.pdfview];
}

-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerbeforeviewcontroller: (Uiviewcontroller *) Viewcontroller
{
Nsuinteger index = [self Indexofviewcontroller: (Pdfviewcontroller *) Viewcontroller];
if (index = = 0) | | (index = = nsnotfound)) {
return nil;
}

index--;
return [self viewcontrolleratindex:index];
}

-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerafterviewcontroller: (Uiviewcontroller *) Viewcontroller
{
Nsuinteger index = [self Indexofviewcontroller: (Pdfviewcontroller *) Viewcontroller];
if (index = = nsnotfound)
{
return nil;
}

index++;

if (index = = [Self.pagepdf count]) {
return nil;
}

return [self viewcontrolleratindex:index];
}


Graffiti effect
mainly related to:
1. Multi-context, layered painting

Copy Code code as follows:

-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX


2. Touch events touches the event of the clan

Copy Code code as follows:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
......


3. Initialize single page view pass page

Copy Code code as follows:

-(ID) initWithFrame: (CGRect) frame onPage: (nsinteger) page


4. Painting Trajectory method

Copy Code code as follows:

Cg_extern void Cgpathmovetopoint (cgmutablepathref path,
Const Cgaffinetransform *m, CGFloat x, cgfloat y)
Cg_extern void Cgpathaddlinetopoint (cgmutablepathref path,
Const Cgaffinetransform *m, CGFloat x, cgfloat y)


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.