-1

我的问题是我必须创建我的屏幕文本的 pdf。我的视图包含一个可滚动的文本视图。我的视图上有许多控件,如日期标签、位置标签等。还有文本视图。我能够创建我的文本视图的 pdf 填充文本,但我不知道如何在我的 pdf 页面中添加日期标签、位置标签和其他带有文本视图的内容。我的 pdf 创建代码是:

CGRect savedFrame = txtView.frame;

UIGraphicsBeginImageContext(txtView.contentSize);
{
    CGPoint savedContentOffset = txtView.contentOffset;

    txtView.contentOffset = CGPointZero;
    txtView.frame = CGRectMake(0, 0, txtJEntry.contentSize.width, txtView.contentSize.height);

    CGRect f = txtView.frame;
    CGContextRef ctx = CGPDFContextCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:newFilePath isDirectory:NO], &f, NULL);

    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -txtView.frame.size.height);
    [txtJEntry.layer renderInContext:ctx];
    CGPDFContextEndPage(ctx);
    CFRelease(ctx);

    txtJEntry.contentOffset = savedContentOffset;
    txtJEntry.frame = savedFrame;
}
UIGraphicsEndImageContext();

如图所示,这是我的视图的屏幕截图。我需要在我的 pdf 中添加天气、位置星星和日期,如图所示,这意味着这些标签的位置在 pdf 中也应该相同。我无法截取整个屏幕的屏幕截图,因为文本视图文本是可滚动的并且有更多数据。

请帮助我如何在我的 pdf 中显示这些内容。

任何建议将不胜感激。提前致谢!

4

1 回答 1

0

据我了解,您希望获取 UIView 的全部内容并将其转换为 PDF。我有一个类别,UIView可以将视图上的所有内容呈现为 PDF 文件。我在我的一个应用程序中成功使用了它。

//UIView+RenderPDF.h
@interface UIView (RenderPDF)
- (void)renderInPDFFile:(NSString*)path;
@end


//UIView+RenderPDF.m
#import "UIView+RenderPDF.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIView (RenderPDF)

- (void)renderInPDFFile:(NSString*)path {

CGRect mediaBox = self.bounds;
CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path isDirectory:NO], &mediaBox, NULL);
CGPDFContextBeginPage(ctx, NULL);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
[self.layer renderInContext:ctx];
CGPDFContextEndPage(ctx);
CFRelease(ctx);
}
@end

现在,为了使用它,一旦你#import UIView+RenderPDF.h要做的就是

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"export.pdf"]];

[someView renderInPDFFile:documentDirectoryFilename];

导致export.pdf被保存到您的Documents目录中。将视图中的所有元素截屏到PDF文件中。

希望这可以帮助!

于 2012-06-06T07:42:32.377 回答