0

我从 Web 服务获取 NSString,使用 dataWithBase64EncodedString 方法将其转换为 NSData,然后使用以下代码将 NSData 转换为 pdf:

CFDataRef myPDFData = (__bridge CFDataRef) retrievedData;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
    CGPDFDocumentRef pdf = (CGPDFDocumentCreateWithProvider(provider));

现在我不知道如何在 iPhone 的屏幕上显示这个 pdf。

+ (id)dataWithBase64EncodedString:(NSString *)string;
{
if (string == nil)
    [NSException raise:NSInvalidArgumentException format:nil];
if ([string length] == 0)
    return [NSData data];

static char *decodingTable = NULL;
if (decodingTable == NULL)
{
    decodingTable = malloc(256);
    if (decodingTable == NULL)
        return nil;
    memset(decodingTable, CHAR_MAX, 256);
    NSUInteger i;
    for (i = 0; i < 64; i++)
        decodingTable[(short)encodingTable[i]] = i;
}

const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
if (characters == NULL)     //  Not an ASCII string!
    return nil;
char *bytes = malloc((([string length] + 3) / 4) * 3);
if (bytes == NULL)
    return nil;
NSUInteger length = 0;

NSUInteger i = 0;
while (YES)
{
    char buffer[4];
    short bufferLength;
    for (bufferLength = 0; bufferLength < 4; i++)
    {
        if (characters[i] == '\0')
            break;
        if (isspace(characters[i]) || characters[i] == '=')
            continue;
        buffer[bufferLength] = decodingTable[(short)characters[i]];
        if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
        {
            free(bytes);
            return nil;
        }
    }

    if (bufferLength == 0)
        break;
    if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
    {
        free(bytes);
        return nil;
    }

    //  Decode the characters in the buffer to bytes.
    bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
    if (bufferLength > 2)
        bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
    if (bufferLength > 3)
        bytes[length++] = (buffer[2] << 6) | buffer[3];
}

realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
4

2 回答 2

1

创建一个上下文,如果您没有任何上下文,然后为给定页面 ( index) 执行以下操作:

- (void)drawPDF:(CGPDFDocumentRef)pdf pageIndex:(NSUInteger)index context:(CGContextRef)ctx {

    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index);
    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                        CGContextGetClipBoundingBox(ctx));
    CGContextConcatCTM(ctx, transform);
    CGContextDrawPDFPage(ctx, page);
}

要获得要绘制的上下文,您可以执行以下操作:

CGSize pageSize = ...;
UIGraphicsBeginImageContextWithOptions(pageSize, YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width, pageSize.height));

    <DRAW PAGE HERE>
    [self drawPDF:pdf pageIndex:N context:context];

UIGraphicsEndImageContext();

编辑,您还应该定义以下 C 函数(在任何类之外),它处理将 PDF 缩放到视图大小:

CGAffineTransform aspectFit(CGRect innerRect, CGRect outerRect) {
  CGFloat scaleFactor = MIN(outerRect.size.width/innerRect.size.width, outerRect.size.height/innerRect.size.height);
  CGAffineTransform scale = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
  CGRect scaledInnerRect = CGRectApplyAffineTransform(innerRect, scale);
  CGAffineTransform translation = 
  CGAffineTransformMakeTranslation((outerRect.size.width - scaledInnerRect.size.width) / 2 - scaledInnerRect.origin.x, 
                                 (outerRect.size.height - scaledInnerRect.size.height) / 2 - scaledInnerRect.origin.y);
  return CGAffineTransformConcat(scale, translation);
}

或者,您可以删除以下行:

    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                        CGContextGetClipBoundingBox(ctx));
    CGContextConcatCTM(ctx, transform);
于 2012-11-29T09:41:11.200 回答
0

您可以在 UIWebview 中加载您的 pdf 文件,该文件将显示其具有许多 pdf 阅读器功能。

于 2012-11-29T10:06:44.530 回答