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)