2009-10-01 3 views
0

안녕하세요 저는 확대/축소 기능이있는 iphone 화면에 pdffile을 표시해야하는 응용 프로그램을 만들고 싶습니다. 나는 여러 페이지의 pdffile을 가지고 있지만, 문제는 내가 한 페이지 만 표시 할 수 있다는 것이다. 여기PDF에서 한 페이지 이상을 표시하려면 어떻게합니까?

/*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 

나는 다음 버튼을 누를 때 나에게 둥지 페이지를 표시해야합니다하지만 같은 page.I는 "CGPDFPageRef"의 페이지 referance에 얻을 저를 표시합니다 증가하지만 표시되지 : 여기 코드입니다 .

Plz 도와주세요.

답변

2

UIWebView을 사용하지 않으시겠습니까? PDF를 렌더링하고 줌 컨트롤을 제공합니다. 바퀴를 재발 명할 필요가 없습니다. Here is documentation for UIWebView.

+0

그러나 이것은 몇 가지 단점을 가지고 있습니다. 하나는, 그가 필요로하는 것보다 훨씬 많은 오버 헤드가 필요하고, 둘째, 누군가가 웹에 들어갈 수있게 해줄 수 있기 때문에 애플이 앱에 17+ 등급을 매길 수있는 기능 영역 근처를 위험에 처한다. –

+0

링크가없는 PDF 만 표시하는 경우 17+ 등급을받을 위험이 없습니다. 많은 앱은 그러한 제한이나 등급이없는 형식화 된 텍스트 디스플레이를 위해 UIWebViews를 사용합니다. –

관련 문제