首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iPad视网膜显示是否有办法减少CGContextBeginPage创建的文件大小?

iPad视网膜显示是否有办法减少CGContextBeginPage创建的文件大小?
EN

Stack Overflow用户
提问于 2013-04-21 04:27:28
回答 1查看 1.2K关注 0票数 2

我正在使用带有Retina 2048x1536分辨率(2x比例尺)的全屏UIViews创建PDF文件。结果文件是非常大的,我得到6-10毫巴文件2页相当简单的视图。理想情况下,我每个文档可以达到4至5页,但目前的方法将使PDF的大小大得令人望而却步。

是否有办法使PDF创建得更小?喜欢创建一个视图截图,将其转换为JPG,然后将其写入PDF上下文?还是我错过了什么高质量的选择?

下面是从单个视图创建PDF的代码:

代码语言:javascript
运行
复制
+(NSMutableData *)createPDFDatafromUIView:(UIView*)aView 
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    return pdfData;
}

+(NSString*)createPDFfromUIView:(UIView*)aView saveToFilepath:(NSString*)filepath
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [AppGraphics createPDFDatafromUIView:aView];


    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:filepath atomically:YES];
    DLog(@"saving PDF to: %@",filepath);
    DLog(@"file exists: %@",[[NSFileManager defaultManager] fileExistsAtPath:filepath]?@"YES":@"NO");
    return filepath;
}

下面是我用来将多个PDF连接在一起的方法

代码语言:javascript
运行
复制
+ (NSString *)joinPDF:(NSArray *)listOfPaths saveToPath:(NSString*)path
{


    CFURLRef pdfURLOutput = (  CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:path]);

    NSInteger numberOfPages = 0;
    // Create the output context
    CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

    for (NSString *source in listOfPaths) {
        CFURLRef pdfURL = (  CFURLRef)CFBridgingRetain([[NSURL alloc] initFileURLWithPath:source]);

        //file ref
        CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
        numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);

        // Loop variables
        CGPDFPageRef page;
        CGRect mediaBox;

        // Read the first PDF and generate the output pages
        DLog(@"GENERATING PAGES FROM PDF 1 (%@)...", source);
        for (int i=1; i<=numberOfPages; i++) {
            page = CGPDFDocumentGetPage(pdfRef, i);
            mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
            CGContextBeginPage(writeContext, &mediaBox);
            CGContextDrawPDFPage(writeContext, page);
            CGContextEndPage(writeContext);
        }

        CGPDFDocumentRelease(pdfRef);
        CFRelease(pdfURL);
    }
    CFRelease(pdfURLOutput);

    // Finalize the output file
    CGPDFContextClose(writeContext);
    CGContextRelease(writeContext);

    return path;
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-21 14:48:56

解决方案确实是在PDF上下文中将图像呈现为JPG。由此产生的压缩PDF大小是较小的-约300-400 is每页!

代码语言:javascript
运行
复制
//aView is used for its bounds property
//screenshot is the screenshot of the view within its bounds
//compression quality 0.55 is 380kb/page 0.66 is 450kb/page
+(NSString*)createPDFDataFromUIView:(UIView*)aView withScreenshot:(UIImage*)screenshot compressionQuality:(float)compression writeToFile:(NSString*)filepath
{

    //prepare JPEG data for rendering into PDF content
    NSData *jpegData = UIImageJPEGRepresentation(screenshot, compression);
    CGDataProviderRef dp = CGDataProviderCreateWithCFData((__bridge CFDataRef)jpegData);
    CGImageRef cgImage = CGImageCreateWithJPEGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);


    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData


    //we need to change the coordinate system, otherwise the image is drawn upside down
    CGContextTranslateCTM(pdfContext, 0, aView.bounds.size.height);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);

    //this creates pixels within PDF context
    CGContextDrawImage(pdfContext, aView.bounds, cgImage);

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    [pdfData writeToFile:filepath atomically:YES];

    //memory cleanup
    CGDataProviderRelease(dp);
    CGImageRelease(cgImage);
    return filepath;

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16127974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档