2010-12-01 34 views
1

i lire les fichiers PDF et affiche également, le code est comme ceci:comment adapter la page de pdf en vue entière

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 

{

NSLog(@"the layer height %d, %d,%d ,%d",layer.bounds.size.width,layer.bounds.size.height,layer.bounds.origin.x,layer.bounds.origin.y); 


int c=[currentPage intValue]+1; 
NSLog(@"drawing started"); 



if(UIInterfaceOrientationIsPortrait([self interfaceOrientation])){ 
    layer.backgroundColor=[UIColor blackColor]; 
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, c); 

    NSLog(@"draw layer"); 
    CGSize pageSize = [kbDataSource pageSize]; 
    GLogInfo(@"the page ize we are getting in draw layer is %@ ",NSStringFromCGSize(pageSize)); 
    //CGContextSaveGState(ctx); 

    CGSize aspectFitSize = [self getPageFitSizeOfSize:pageSize inSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height- __TOP_BRANDING_BAR_HEIGHT)]; 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 



    GLogDebug(@"aspect fitsize of image to %@", NSStringFromCGSize(aspectFitSize)); 
    NSLog(@"layer bounds are:%@ %@ ",NSStringFromCGSize(layer.bounds.size),NSStringFromCGPoint(layer.bounds.origin)); 
    NSLog(@"page size of the book is:%@",NSStringFromCGSize(pageSize)); 
    CGFloat aspectRatio=pageSize.width/pageSize.height; 
/* 
    if (aspectRatio>0.5 && aspectRatio<=0.74) { 
     NSLog(@"aspect ratio is between 0.5 and 0.74"); 
     CGContextTranslateCTM(ctx,-110 ,layer.bounds.size.height+100); 
     CGContextScaleCTM(ctx, 1.28,-1.22); 
    } 
    else 
     if (aspectRatio>0.74 && aspectRatio<1) { 
      NSLog(@"aspect ratio is between 0.75 and 1"); 
      //CGContextTranslateCTM(ctx, -145,layer.bounds.size.height+85); 
      //CGContextScaleCTM(ctx, 1.4,-1.2); 
      CGContextTranslateCTM(ctx, -130,layer.bounds.size.height+75); 
      CGContextScaleCTM(ctx, 1.35,-1.18); 

     } 
     else if(aspectRatio>1) { 
      NSLog(@"aspect ratio is above 1"); 
      CGContextTranslateCTM(ctx,-80,layer.bounds.size.height+80); 
      CGContextScaleCTM(ctx, 1.2,-1.2); 
     } 
     else { 
      CGContextTranslateCTM(ctx, 0, layer.bounds.size.height); 
      CGContextScaleCTM(ctx, 1.0, -1.0); 
     }*/ 


/* 
    CGFloat scalingRatio; 


    if ((layer.bounds.size.width/pageSize.width)<((layer.bounds.size.height-41)/pageSize.height)){ 

    scalingRatio=layer.bounds.size.width/pageSize.width; 

    } 
    else { 
     scalingRatio=(layer.bounds.size.height-41)/pageSize.height; 
    } 

    */ 
    CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox); 
    CGRect targetRect = layer.bounds; 
    CGFloat xScale = targetRect.size.width/cropBox.size.width; 
    CGFloat yScale = (targetRect.size.height-41)/cropBox.size.height; 
    CGFloat scaleToApply = (xScale < yScale) ? xScale : yScale; 

    CGContextTranslateCTM(ctx, 0.0, -41+layer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 
    NSLog(@"the crop box values are %@",NSStringFromCGRect(cropBox)); 
    NSLog(@"the crop box values are %f",cropBox.origin.x); 

    NSLog(@"the scaleToApply is %f",scaleToApply); 
    NSLog(@"the view bounds are %@",[self.view description]); 

    if (scaleToApply == yScale) 
     CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-100, -150)); 
    else 
     CGContextConcatCTM(ctx, CGAffineTransformMakeTranslation(-180, -260)); //evans 
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply)); 
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)); 
    // 
    //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply)); 


    CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault); 
    CGContextDrawPDFPage(ctx, myPageRef); 
    //CGContextRestoreGState(ctx); 

    [loadingIndicator stopAnimating]; 
    [loadingIndicator1 stopAnimating]; 
    [loadingIndicator2 stopAnimating]; 

} 

il lit & affiche pdf exactement, mais le problème est , il n'est pas adapté à la vue entière pour chaque pdf, y a-t-il une modification ou un besoin de correction dans mon code. S'il vous plaît poster quelques suggestions et commentaires. Je vous remercie!!!

+0

duplication possible de [Afficher le PDF sur l'iPad] (http://stackoverflow.com/questions/4722526/display-pdf-on-ipad) – outis

+0

Vous pouvez voir le lien, je pense que cela pourrait vous être utile ... [lien texte] (http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref/doc/uid/TP30001066-CH214-TPXREF109) – iPhoneDv

+0

pouvez-vous donnez la réponse pour le code modifié. – ajay

Répondre

1

Vous devez calculer les valeurs d'échelle dont vous avez besoin pour que la page corresponde à votre vue. Ensuite, mettez les bonnes valeurs dans votre

CGContextScaleCTM(ctx, 1.0, -1.0); 

appel.

+0

merci altealice ça marche – ajay

+0

@ajay, étiez-vous capable d'écrire une méthode pour calculer les valeurs d'échelle qui fonctionne dans les deux modes d'orientation? Si oui, pouvez-vous le poster comme une mise à jour dans votre question? Merci! –

+1

@ Flash84x c'est normalement un ratio de dimensions de votre page et les dimensions de vue/trame où vous voulez les afficher, où 100% = 1.0. Devrait être assez facile à calculer. – Altealice