2011-09-26 6 views
0

iOS 용 그리기 및 인쇄 가이드 (http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#)를 읽고 있습니다. // apple_ref/doc/uid/TP40010156-CH10-SW1). 내가 필요한 것을 얻기 위해 조금 수정하려고하고있다. 기본적으로 색이 지정된 사각형, NSString에서 텍스트, 다른 색 사각형, NSString에서 텍스트의 두 번째 블록을 그리려고합니다. 각 문자열에 하나씩 두 개의 프레임 세트를 만들고 renderPage : 메서드를 두 번 호출합니다. 그러나 화면 아래쪽에 텍스트를 거꾸로 그립니다. 이유는 모르겠습니다. (코드가 왜 그런 식으로 작동하는지, 왜 작동하지 않는지 이해하지 못하면 어떻게해야 하는지를 알기 위해 주석에 몇 가지 질문을 넣었습니다.) 감사!석영에서 PDF 생성 도움말

CFAttributedStringRef overviewText = CFAttributedStringCreate(NULL, (CFStringRef)overview, NULL); 
    CFAttributedStringRef resultText = CFAttributedStringCreate(NULL, (CFStringRef)result, NULL); 
    if (overviewText != NULL || resultText != NULL) { 
     CTFramesetterRef overviewFramesetter = CTFramesetterCreateWithAttributedString(overviewText); 
     CTFramesetterRef resultFramesetter = CTFramesetterCreateWithAttributedString(resultText); 
     if (overviewFramesetter != NULL || resultFramesetter != NULL) { 
      // Create the PDF context using the default page size of 612 x 792. 
      UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil); 

      CFRange currentRange = CFRangeMake(0, 0); 
      NSInteger currentPage = 0; 
      BOOL done = NO; 

      do { 
       // Mark the beginning of a new page. 
       UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, PDF_WIDTH, PDF_HEIGHT), nil); 

       [self drawPDFTitle:fileName]; 
       [self drawRectangleAtPosition:CGPointMake(MARGIN, 50)]; 

       // Draw a page number at the bottom of each page 
       currentPage++; 
       [self drawPageNumber:currentPage]; 

       // Render the current page and update the current range to 
       // point to the beginning of the next page. 
       currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:overviewFramesetter]; 
       NSLog(@"%ld, %ld", currentRange.location, currentRange.length); 

      // at first I tried doing this, but would get an error at the CFRelease in renderPage method. I'm not sure as to why I get the error since the function renderPage: seems to be the method to write the data to the PDF context. 
currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:resultFramesetter]; 

       // If we're at the end of the text, exit the loop. 
       if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)overviewText)) 
        done = YES; 
      } 
      while (!done); 

      do { 
       // I do not know why I would need to flip the context again since the method renderPage: already does this for me right? 
       //CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, PDF_HEIGHT); 
       //CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0); 
       currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:resultFramesetter]; 
       NSLog(@"2nd loop: %ld, %ld", currentRange.location, currentRange.length); 
       if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)overviewText)) 
        done = YES; 
      } 
      while (!done); 

      // Close the PDF context and write the contents out. 
      UIGraphicsEndPDFContext(); 

      // Release the framewetter. 
      CFRelease(overviewFramesetter); 

     } 

답변

0

PDF 파일 좌표 시스템은 석영 좌표계 (y 축이 뒤집혀)에서 다르기 때문에 당신은 PDF 좌표를 변환해야합니다. 자세한 내용은 http://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_overview/dq_overview.html (Quartz 2D 좌표계)을 참조하십시오.

다음과 같은 코드 -

CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height); 
CGContextScaleCTM(pdfContext, 1.0, -1.0); 

HTH를 사용하여 번역 할 수 있습니다,

하기 Akshay