2010-02-13 15 views
5

मैं नीचे दिए गए कोड का उपयोग करके पीडीएफ के पेज खींचने की कोशिश कर रहा हूं। कुछ पीडीएफ सही ढंग से प्रस्तुत करते हैं, लेकिन अन्य बस पूरी तरह से काले छवि के रूप में दिखाते हैं, या आंशिक भाग प्रदान किए जाते हैं और बाकी का काला होता है। क्या हो रहा है इसकी तुलना में, जो लोग ठीक दिखाते हैं, उनमें हमेशा कुछ ग्राफिक्स (आरेख, इत्यादि) के साथ "नियमित" टेक्स्ट होता है, जबकि काले रंग से बाहर आने वाले लोग आमतौर पर सभी ग्राफिक्स होते हैं (शीट के एक पृष्ठ की तरह) संगीत, उदाहरण के लिए)। क्या कोई मुझे इस पर सही दिशा में इंगित कर सकता है? मैं इसे नए 3.2 एसडीके पर बना रहा हूं।आईफोन कुछ पीडीएफ काले छवि के रूप में प्रतिपादन

धन्यवाद!

// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system 
// before we start drawing. 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

// Grab the first PDF page 
CGPDFPageRef page = CGPDFDocumentGetPage(myPDF, pageNo); 
// We're about to modify the context CTM to draw the PDF page where we want it, so save  the graphics state in case we want to do more drawing 
CGContextSaveGState(context); 
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any 
// base rotations necessary to display the PDF page correctly. 
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true); 
// And apply the transform. 
CGContextConcatCTM(context, pdfTransform); 
// Finally, we draw the page and restore the graphics state for further manipulations! 
CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context); 
+0

इसे आज़माएं http://www.random-ideas.net/posts/42 –

उत्तर

4

CGPDFDocumentRef MyGetPDFDocumentRef (कॉन्स char * फ़ाइल नाम) { CFStringRef पथ; CFURLRef url; CGPDFDocumentRef दस्तावेज़; पथ = CFStringCreateWithCString (NULL, फ़ाइल नाम, kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath (NULL, पथ, kCFURLPOSIXPathStyle, 0); CFRelease (पथ); दस्तावेज़ = CGPDFDocumentCreateWithURL (url); // 2 CFRelease (url); int count = CGPDFDocumentGetNumberOfPages (दस्तावेज़); // 3 अगर (गणना == 0) { printf ("`% s 'को कम से कम एक पृष्ठ की आवश्यकता है! ", फ़ाइल नाम); वापस लौटें; } रिटर्न दस्तावेज़; }

void MyDisplayPDFPage (CGContextRef myContext,size_t pageNumber,const char *filename) { 
    CGPDFDocumentRef document; 
    CGPDFPageRef page; 
    document = MyGetPDFDocumentRef (filename);// 1 
    page = CGPDFDocumentGetPage (document, pageNumber);// 2 
    CGContextDrawPDFPage (myContext, page);// 3 
    CGContextTranslateCTM(myContext, 0, 20); 
    CGContextScaleCTM(myContext, 1.0, -1.0); 
    CGPDFDocumentRelease (document);// 4 
} 



#import "DashBoard.h" 

#import <QuartzCore/QuartzCore.h> 

@interface UIImage (WBImage) 
// rotate UIImage to any angle 
-(UIImage*)rotate:(UIImageOrientation)orient; 
@end 

@implementation UIImage (WBImage) 


static inline CGFloat degreesToRadians(CGFloat degrees) 
{ 
    return M_PI * (degrees/180.0); 
} 

static inline CGSize swapWidthAndHeight(CGSize size) 
{ 
    CGFloat swap = size.width; 

    size.width = size.height; 
    size.height = swap; 

    return size; 
} 


-(UIImage*)rotate:(UIImageOrientation)orient 
{ 
    CGRect    bnds = CGRectZero; 
    UIImage*   copy = nil; 
    CGContextRef  ctxt = nil; 
    CGRect    rect = CGRectZero; 
    CGAffineTransform tran = CGAffineTransformIdentity; 

    bnds.size = self.size; 
    rect.size = self.size; 

    switch (orient) 
    { 
     case UIImageOrientationUp: 
      return self; 

     case UIImageOrientationUpMirrored: 
      tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0); 
      tran = CGAffineTransformScale(tran, -1.0, 1.0); 
      break; 

     case UIImageOrientationDown: 
      tran = CGAffineTransformMakeTranslation(rect.size.width, 
                rect.size.height); 
      tran = CGAffineTransformRotate(tran, degreesToRadians(180.0)); 
      break; 

     case UIImageOrientationDownMirrored: 
      tran = CGAffineTransformMakeTranslation(0.0, rect.size.height); 
      tran = CGAffineTransformScale(tran, 1.0, -1.0); 
      break; 

     case UIImageOrientationLeft: 
      bnds.size = swapWidthAndHeight(bnds.size); 
      tran = CGAffineTransformMakeTranslation(0.0, rect.size.width); 
      tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0)); 
      break; 

     case UIImageOrientationLeftMirrored: 
      bnds.size = swapWidthAndHeight(bnds.size); 
      tran = CGAffineTransformMakeTranslation(rect.size.height, 
                rect.size.width); 
      tran = CGAffineTransformScale(tran, -1.0, 1.0); 
      tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0)); 
      break; 

     case UIImageOrientationRight: 
      bnds.size = swapWidthAndHeight(bnds.size); 
      tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0); 
      tran = CGAffineTransformRotate(tran, degreesToRadians(90.0)); 
      break; 

     case UIImageOrientationRightMirrored: 
      bnds.size = swapWidthAndHeight(bnds.size); 
      tran = CGAffineTransformMakeScale(-1.0, 1.0); 
      tran = CGAffineTransformRotate(tran, degreesToRadians(90.0)); 
      break; 

     default: 
      // orientation value supplied is invalid 
      assert(false); 
      return nil; 
    } 

    UIGraphicsBeginImageContext(bnds.size); 
    ctxt = UIGraphicsGetCurrentContext(); 

    switch (orient) 
    { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      CGContextScaleCTM(ctxt, -1.0, 1.0); 
      CGContextTranslateCTM(ctxt, -rect.size.height, 0.0); 
      break; 

     default: 
      CGContextScaleCTM(ctxt, 1.0, -1.0); 
      CGContextTranslateCTM(ctxt, 0.0, -rect.size.height); 
      break; 
    } 

    CGContextConcatCTM(ctxt, tran); 
    CGContextDrawImage(ctxt, rect, self.CGImage); 

    copy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return copy; 
} 


@end 




@implementation DashBoard 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    filePath=[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"roadie" ofType:@"pdf"]]; 

    // an IBOutlet UIScrollView 
    [scrMain setContentSize:CGSizeMake(720, 720)]; 
    // an IBOutlet UIImageView 
    [imgV setFrame:CGRectMake(0, 0, 720, 720)]; 
    UIGraphicsBeginImageContext(CGSizeMake(720, 720)); 
    // initial page is an size_t variable 
    initialPage=1; 
    MyDisplayPDFPage(UIGraphicsGetCurrentContext(), initialPage, [filePath UTF8String],myTable,mainString); 
    imgV.image=UIGraphicsGetImageFromCurrentImageContext(); 
    imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored]; 
} 

बस कोड ऊपर & कॉपी पेस्ट करें। यह आपके लिए काम करेगा।

+0

धन्यवाद सागर .... बहुत बढ़िया – skantner

3

समस्या पृष्ठभूमि रंग थी - विशेष रूप से, पीडीएफ मुझे परेशानी हो रही थी जिसमें सफेद पृष्ठभूमि नहीं थी। एक सफेद आयताकार के साथ दृश्य भरकर, मेरी समस्या हल हो गई थी।

संबंधित मुद्दे