2011-03-15 103 views
0

由於我是iphone領域的新手,我想要一些如何在iphone中製作PDF閱讀器的最佳教程。任何人都可以告訴我如何使iPhone PDF閱讀器從互聯網上查看本地PDF和PDF文件?iphone中的PDF閱讀器

在此先感謝。

回答

4
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPDFFile" ofType:@"pdf"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    NSURLRequest * request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 
    [webView setScalesPageToFit:YES]; 

} 
2
#import <UIKit/UIKit.h> 


@interface TiledPDFView : UIView { 
    CGPDFPageRef pdfPage; 
    CGFloat myScale; 

} 

- (id)initWithFrame:(CGRect)frame andScale:(CGFloat)scale; 
- (void)setPage:(CGPDFPageRef)newPage; 

@end 



#import "TiledPDFView.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation TiledPDFView 


// Create a new TiledPDFView with the desired frame and scale. 
- (id)initWithFrame:(CGRect)frame andScale:(CGFloat)scale{ 
    if ((self = [super initWithFrame:frame])) { 

     CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; 

     tiledLayer.levelsOfDetail = 4; 
     tiledLayer.levelsOfDetailBias = 4; 
     tiledLayer.tileSize = CGSizeMake(512.0, 512.0); 
     myScale = scale; 
    } 
    return self; 
} 

// Set the layer's class to be CATiledLayer. 
+ (Class)layerClass { 
    return [CATiledLayer class]; 
} 

// Set the CGPDFPageRef for the view. 
- (void)setPage:(CGPDFPageRef)newPage 
{ 
    CGPDFPageRelease(self->pdfPage); 
    self->pdfPage = CGPDFPageRetain(newPage); 
} 

-(void)drawRect:(CGRect)r 
{ 
    // UIView uses the existence of -drawRect: to determine if it should allow its CALayer 
    // to be invalidated, which would then lead to the layer creating a backing store and 
    // -drawLayer:inContext: being called. 
    // By implementing an empty -drawRect: method, we allow UIKit to continue to implement 
    // this logic, while doing our real drawing work inside of -drawLayer:inContext: 
} 


// Draw the CGPDFPageRef into the layer at the correct scale. 
-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context 
{ 
    // First fill the background with white. 
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,0.5); 
    CGContextFillRect(context,self.bounds); 

    CGContextSaveGState(context); 
    // Flip the context so that the PDF page is rendered 
    // right side up. 
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 


    // Scale the context so that the PDF page is rendered 
    // at the correct size for the zoom level. 
    CGContextScaleCTM(context, myScale,myScale);  
    CGContextDrawPDFPage(context, pdfPage); 
    CGContextRestoreGState(context); 

} 

// Clean up. 
- (void)dealloc { 
    CGPDFPageRelease(pdfPage); 

    [super dealloc]; 
} 


@end 

添加這個視圖到您的視圖控制器

問候, 希亞姆帕爾馬

+2

這可能會幫助你 http://developer.apple.com/library/ios/#samplecode /ZoomingPDFViewer/Introduction/Intro.html – GameLoading 2011-03-15 06:21:18