首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Obj视图到PDF只呈现第一页

Obj视图到PDF只呈现第一页
EN

Stack Overflow用户
提问于 2012-05-04 17:51:10
回答 1查看 2.5K关注 0票数 3

我有一个UIView扩展类,名为UIView+PDFView,它接受当前视图,将其拆分为页面,并呈现一个PDF文档。我的问题在于呈现部分;我可以成功地为内容“创建”页面,但是第一个页面之后的所有页面都是空白的。我的最终目标是获取当前视图,“缩放”与页面相等的宽度,并在PDF中对视图的其余部分进行分页。

问题不在于:

  • 将PDF附加到电子邮件
  • ,将PDF发送到打印机/打印模拟器
  • ,而实际上生成的PDF
  • 调整了视图(SO Question 4919388)
    • 的大小,然后再发送到方法。通过使框架10 by高来验证,仍然打印一个(而且只有一个)完整的page

  • 正确地翻译视图。通过同时进行翻译和缩放来验证;两者都正确地更改了视图,但是在第一页上呈现的内容都不多。

我的代码如下:

代码语言:javascript
复制
@interface UIView (PDFView)
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo;
@end

@implementation UIView (PDFView)

-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo {
    // http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html#//apple_ref/doc/constant_group/Auxiliary_Dictionary_Keys

    // 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
    CGSize pageSize = CGSizeMake(self.bounds.size.width, 792);
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, documentInfo);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    NSInteger currentPage = 0;
    BOOL done = NO;

    do {
        CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
        UIGraphicsBeginPDFPageWithInfo(currentPageRect, nil);

        // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
        [self.layer renderInContext:pdfContext];

        // If we're at the end of the view, exit the loop.
        if ((pageSize.height*currentPage) > self.bounds.size.height) {
            done = YES;
        } else {
            currentPage++;
        }
    } while (!done);

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    if (aFilename == nil) {
        return pdfData;
    } else {
        // Retrieves the document directories from the iOS device
        NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

        NSString* documentDirectory = [documentDirectories objectAtIndex:0];
        NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

        // instructs the mutable data object to write its context to a file on disk
        [pdfData writeToFile:documentDirectoryFilename atomically:YES];
        NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
        return nil;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-04 18:11:12

我认为你的问题在于

代码语言:javascript
复制
CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);

而不是这样,尝试使用以下任何一条语句

代码语言:javascript
复制
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);
UIGraphicsBeginPDFPage();

每次您希望向上下文中添加一个新页面时,请使用上述语句,并且您将能够将页面添加到上下文中。

如果您希望使用页面的默认大小,即612X792,则可以直接使用UIGraphicsBeginPDFPage();

对于自定义页面大小,可以使用UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);

我觉得这应该能解决你的问题。

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

https://stackoverflow.com/questions/10453733

复制
相关文章

相似问题

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