iphone - 在 iPad 应用程序中从 UIView 创建 pdf 的问题

标签 iphone ios ipad uiview airprint

我正在我的 iPad 应用程序中从 UIView 创建一个 pdf。它的大小为 768 * 2000。当我创建 pdf 时,它会以相同的大小创建,并在一页上显示所有内容。所以当我从 iPad 打印时我遇到了问题。我正在使用以下代码创建pdf:-

-(void)drawPdf:(UIView *)previewView{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068);
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
    CGContextBeginPage (pdfContext,nil); // 6

    //turn PDF upsidedown

    CGAffineTransform transform = CGAffineTransformIdentity;    
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(pdfContext, transform);

    //Draw view into PDF
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);  
}

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

                                     path,

                                     kCFURLPOSIXPathStyle,

                                     false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

                                              &inMediaBox,                                                NULL);        
        CFRelease(url);// 3     
    }   
    return myOutContext;// 4    
} 

谁能建议我如何减小 pdf 大小并且它有多个页面?

提前致谢。

最佳答案

请参阅“iOS 绘图和打印指南”中的示例

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

基本上在 list 4-1 代码示例中,他们有一个 do while 循环,并注意它是如何在循环中启动一个新的 PDF 页面的:

...

// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

...

在您当前的方法中,您只调用了一次开始页面方法之一,这就是为什么您只会有一个页面的原因。

关于iphone - 在 iPad 应用程序中从 UIView 创建 pdf 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6068034/

相关文章:

ios - 在日历中的日期之间添加多个事件 - IOS

ios - 使用 sendAsynchronousRequest :queue:completionHandler: 处理 401 响应

jquery - 同时播放背景音乐和音效

iphone - 如何在 iPhone UIView 中使用负号框架大小?

iphone - 从 ABPeoplePickerNavigationController 中过滤掉电子邮件地址

ios - iphone ios 上的 swift 3 中的弹出窗口

ios - swift 中的协议(protocol)和委托(delegate)问题

ios - 从 viewcontroller 获取 appdelegate 值

应用委托(delegate)中出现平面问题时的 iPad 启动方向

ios - Collection View ,具有自定义布局,单元格在滚动时行为异常