2014-11-04 3 views
2

У меня есть приложение, которое с радостью создает PDF-файлы с использованием кварца/UIKit с iOS 4, но с момента обновления проекта до iOS 8 происходит сбой при попытке визуализации текста в контексте PDF. Рисование линий & прямоугольников отлично, но любая перестановка строкового рендеринга завершается с исключением в одной из библиотек низкого уровня.IOS 8 API печать PDF-файлов: сломанный при рисовании текста?

Вместо того, чтобы публиковать собственный источник, я пробовал работать в обратном направлении из документации Apple. Конечно, это устарело, но если он больше не должен работать, они должны были его исправить.

https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html

адаптированный исходный код:

- (void)producePDF 
{ 
    NSString *[email protected]"Bzorg blarf gloop foo!"; 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)text, NULL); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 

    NSString *pdfFileName = fullPath; 
    // Create the PDF context using the default page size of 612 x 792. 
    UIGraphicsBeginPDFContextToFile(pdfFileName, 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, 612, 792), nil); 

     // 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 renderPageWithTextRange:currentRange andFramesetter:framesetter]; 
     currentRange=[self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter]; 

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

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

    // Release the framewetter. 
    CFRelease(framesetter); 

    // Release the attributed string. 
    CFRelease(currentText); 
} 

- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange 
       andFramesetter:(CTFramesetterRef)framesetter 
{ 
    // Get the graphics context. 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // Put the text matrix into a known state. This ensures 
    // that no old scaling factors are left in place. 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 

    // Create a path object to enclose the text. Use 72 point 
    // margins all around the text. 
    CGRect frameRect = CGRectMake(72, 72, 468, 648); 
    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 

    // Get the frame that will do the rendering. 
    // The currentRange variable specifies only the starting point. The framesetter 
    // lays out as much text as will fit into the frame. 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 

    // Core Text draws from the bottom-left corner up, so flip 
    // the current transform prior to drawing. 
    CGContextTranslateCTM(currentContext, 0, 792); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    // Draw the frame. 
    CTFrameDraw(frameRef, currentContext); 

    // Update the current range based on what was drawn. 
    currentRange = CTFrameGetVisibleStringRange(frameRef); 
    currentRange.location += currentRange.length; 
    currentRange.length = 0; 
    CFRelease(frameRef); 

    return currentRange; 
} 

Я пробовал многочисленные перестановки, и все они, похоже, не в точной точке рендеринга текста. Apple, полученный выше пример умирает на линии:

CTFrameDraw(frameRef, currentContext); 

Другой код пытается получить минимальную работу:

NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy; 
textStyle.alignment = NSTextAlignmentLeft; 
NSDictionary* textFontAttributes = @{ 
    NSFontAttributeName: [UIFont fontWithName: @"Helvetica" size: 12], NSForegroundColorAttributeName: UIColor.redColor, 
    NSParagraphStyleAttributeName: textStyle}; 
[@"Hello, World!" drawAtPoint:CGPointZero withAttributes:textFontAttributes]; 

... сбой при вызове «drawAtPoint».

ответ

1

Для чего это стоит, если я выполняю приложение на устройстве без приложенного отладчика (т. Е. Запускать/убивать/запускать с трамплина), создание PDF работает очень хорошо. Предположительно, какое бы ни было ложное исключение, он просто игнорируется в реальной жизни.

Смежные вопросы