将Nsdata转换为PDF,而无需写入文档目录中的该文件

时间:2018-09-04 07:45:30

标签: ios objective-c xcode

从服务器端获取加密的数据文件后,我将其解密并将其保存在文档目录中,并在PDF阅读器中显示。一切正常。

 {
   CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);

    int i = (int) CGPDFDocumentGetNumberOfPages(document);
    NSLog(@"Number Of pages: %d",i);

    //Create the pdf context
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

    if (CGPDFDocumentGetNumberOfPages(document) > 0)
    {
        //Draw the page onto the new context
        //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1

        for (int j = 0; j < i; j++) {
            page = CGPDFDocumentGetPage(document, j+1);
            CGPDFContextBeginPage(pdfContext, NULL);
            CGContextDrawPDFPage(pdfContext, page);
            CGPDFContextEndPage(pdfContext);
        }
    }
    else
    {
        NSLog(@"Failed to create the document");
    }
    CGContextRelease(pdfContext); //Release before writing data to disk.

    //Write to disk

    [Utility saveFileInDocumentDirectory:(__bridge NSData *)mutableData fileName:[NSString stringWithFormat:@"%@.pdf",_fileName] inDirectory:[NSString stringWithFormat:@"%@_LibraryPDF",mUser.id]];

    //CleanUP
    CGDataConsumerRelease(dataConsumer);
    CGPDFDocumentRelease(document);
    CFRelease(mutableData);

    [self showPDF:[NSString stringWithFormat:@"%@.pdf",_fileName]];
}

-(void)showPDF:(NSString *)_fileName// data:(NSData *)_data
{
    NSString *strPDF = [Utility getFilePathFromDocumentDirectory:_fileName inFolder:[NSString stringWithFormat:@"%@_LibraryPDF",mUser.id]];
    NSLog(@"%@",strPDF);
    ReaderDocument *document = [ReaderDocument withDocumentFilePath:strPDF password:nil];
    if (document != nil){
        ReaderViewController *mReaderViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
        mReaderViewController.delegate = self; // Set the LazyPDFViewController delegate to self
        [self.navigationController pushViewController:mReaderViewController animated:NO];
    }

}

但是我的问题是我可以在不保存pdf文件文档目录的情况下在PDF阅读器中显示解密的数据文件吗?

1 个答案:

答案 0 :(得分:0)

CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);

int i = (int) CGPDFDocumentGetNumberOfPages(document);
NSLog(@"Number Of pages: %d",i);

//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
    //Draw the page onto the new context
    //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1

    for (int j = 0; j < i; j++) {
        page = CGPDFDocumentGetPage(document, j+1);
        CGPDFContextBeginPage(pdfContext, NULL);
        CGContextDrawPDFPage(pdfContext, page);
        CGPDFContextEndPage(pdfContext);
    }
}
else
{
    NSLog(@"Failed to create the document");
}
CGContextRelease(pdfContext); //Release before writing data to disk.

//Write to disk

//[Utility saveFileInDocumentDirectory:(__bridge NSData *)mutableData fileName:[NSString stringWithFormat:@"%@.pdf",_fileName] inDirectory:[NSString stringWithFormat:@"%@_LibraryPDF",mUser.id]];

//CleanUP
CGDataConsumerRelease(dataConsumer);
CGPDFDocumentRelease(document);
[self showPDF:[[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding]];
//CFRelease(mutableData); //- you might need to re-enable this line

尝试一下。