Iphone 如何显示PDF中的多个页面?

Iphone 如何显示PDF中的多个页面?,iphone,objective-c,Iphone,Objective C,您好,我想制作一个应用程序,其中我必须在iphone屏幕上显示pdffile,它具有缩放功能。我有多页的pdffile,但问题是我只能显示一页。 代码如下: /*myView.m*/ @implementation MyView - (void)configureTiledLayer { if([global getfirsttime] == 0) { [global fetchpageCtr : 1]; [global fetchfir

您好,我想制作一个应用程序,其中我必须在iphone屏幕上显示pdffile,它具有缩放功能。我有多页的pdffile,但问题是我只能显示一页。 代码如下:

/*myView.m*/

@implementation MyView

- (void)configureTiledLayer {

    if([global  getfirsttime] == 0)
    {

        [global fetchpageCtr : 1];
        [global fetchfirsttime:1];
    }

  zoom = 1.0f;

  tiledLayer = [CATiledLayer layer];
  TiledDelegate *delegate = [[TiledDelegate alloc] init];
  tiledLayer.delegate = delegate;
  // get tiledLayer size
  CGRect pageRect = CGPDFPageGetBoxRect(delegate.map, kCGPDFCropBox);
  int w = pageRect.size.width;
  int h = pageRect.size.height;


    NSLog(@"height==%d,weight=%d",h,w);
  // get level count
  int levels = 1;
  while (w > 1 && h > 1) {
    levels++;
    w = w >> 1;
    h = h >> 1;
  }


    NSLog(@"Layer create");

  // set the levels of detail
  tiledLayer.levelsOfDetail = levels;
  // set the bias for how many 'zoom in' levels there are
  tiledLayer.levelsOfDetailBias = 5;
  // setup the size and position of the tiled layer
        CGFloat width = CGRectGetWidth(pageRect);
        CGFloat height = CGRectGetHeight(pageRect);
  tiledLayer.bounds = CGRectMake(0.0f, 0.0f, width, height);
  CGFloat x = width * tiledLayer.anchorPoint.x;
  CGFloat y = -height * tiledLayer.anchorPoint.y;
  tiledLayer.position = CGPointMake(x * zoom, y * zoom);
  tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);

  // transform the super layer so things draw 'right side up'
  CATransform3D superTransform = CATransform3DMakeTranslation(0.0f, self.bounds.size.height, 0.0f);
  self.layer.transform = CATransform3DScale(superTransform, 1.0, -1.0f, 1.0f);

  [self.layer addSublayer:tiledLayer];


  [tiledLayer setNeedsDisplay];
  moving = NO;

    NSLog(@"in layer");
}  

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
      self.backgroundColor = [UIColor blueColor];
    [self configureTiledLayer];
  }
  return self;
}

- (id)initWithCoder:(NSCoder *)coder {
  if (self = [super initWithCoder:coder]) {


    [self configureTiledLayer];

  }
  return self;
}

- (void)setZoom:(CGFloat)newZoom {
  zoom = newZoom;
  tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);
}

- (void)zoomIn {
  [self setZoom:zoom * 2.0f];
}

- (void)zoomOut {
  [self setZoom:zoom * 0.5f];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  if(touches.count == 1) {
    previousPoint = [[touches anyObject] locationInView:self];
  } else if(touches.count == 2) {
    // pinch zoom
    pinchZoom = YES;
    NSArray *touches = [event.allTouches allObjects];
    CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
    CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
    previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) + 
                            pow(pointOne.y - pointTwo.y, 2.0f));
  }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if(touches.count == 1) {
    CGPoint currentPoint = [[touches anyObject] locationInView:self];
    CGPoint delta = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
    tiledLayer.position = CGPointMake(tiledLayer.position.x + delta.x * zoom,
                                      tiledLayer.position.y + delta.y * zoom);
    previousPoint = currentPoint;
    moving = YES;
  } else if(touches.count == 2) {
    // pinch zoom stuff
    NSArray *touches = [event.allTouches allObjects];
    CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
    CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
    CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) + 
                            pow(pointOne.y - pointTwo.y, 2.0f));
    CGFloat newZoom = fabs(zoom + (distance - previousDistance) / previousDistance);
    [self setZoom:newZoom];
    previousDistance = distance;
  }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  if(!moving) {
    if(touches.count == 1) {
      // realy should recenter on a click but I'm being lazy
      if([[touches anyObject] tapCount] == 2) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        [self zoomOut];
      } else {
        [self performSelector:@selector(zoomIn) withObject:nil afterDelay:0.25];
      }
    }
  } else {
    moving = NO;
  }
}

- (void)dealloc {
  [tiledLayer release];
  [super dealloc];
}

/*TiledDelegate.m*/
@implementation TiledDelegate

- (CGPDFDocumentRef)sfMuni {
  if(NULL == sfMuni) {


    NSString *path = [[NSBundle mainBundle] pathForResource:@"Hunting-TrappingSynopsis_0910" ofType:@"pdf"];
    NSURL *docURL = [NSURL fileURLWithPath:path];
    sfMuni = CGPDFDocumentCreateWithURL((CFURLRef)docURL);
  }
  return sfMuni;
}

- (CGPDFPageRef)map {

    int temppageno  = [global getpageCtr];

    NSLog(@"page ctr ==%d ",temppageno);

    if(NULL == map) {
        map = CGPDFDocumentGetPage(self.sfMuni, temppageno);
    }
    return map;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  NSLog(@"\ndrawLayer:inContext:");
  NSLog(@"ctm = %@", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
  NSLog(@"box = %@\n", NSStringFromCGRect(CGContextGetClipBoundingBox(ctx)));
  CGContextDrawPDFPage(ctx, self.map);


}

- (void)dealloc {
  CGPDFPageRelease(map);
  CGPDFDocumentRelease(sfMuni);
  [super dealloc];
}

/*TiledLayerAppDelegate*/
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

/*TiledLayerViewController*/

- (void)viewDidLoad 
{

    l_pagectr = 2;

    UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    btn1.frame = CGRectMake(0,0,70,50);
    [btn1 setBackgroundColor: [UIColor whiteColor]];
    btn1.exclusiveTouch = YES;
    [btn1 setTitle:@"Next" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(NextPressed:)
   forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];



}


-(IBAction)NextPressed : (id)sender
{

    [global fetchpageCtr : l_pagectr++];



    MyView *myview1=[[MyView alloc]init];

}
@end
在这里,当我按下“下一步”按钮时,它将显示嵌套页面,但将显示相同的页面。我还将在“CGPDFPageRef”中增加页面引用,但不显示


请帮助我。

为什么不直接使用
UIWebView
?它呈现PDF并提供缩放控件。不需要重新发明轮子

不过,这确实有几个缺点。第一,它需要的开销比他看起来需要的要大得多;第二,它危险地接近苹果会让你在应用程序上获得17+评级的功能区域,因为它可能会让人上网。如果你只显示没有链接的PDF,就没有获得17+评级的危险。例如,许多应用程序使用UIWebView进行格式化文本显示,但没有这样的限制或评级。