【问题标题】:Drawing PDF from two views从两个视图绘制 PDF
【发布时间】:2014-08-13 14:48:20
【问题描述】:

我有第一个视图firstView,还绘制了第二个视图secondView,并将其作为子视图添加到firstView。 我也有从firstView PDF 文件创建的方法:

-(void)createPDFfromUIView:(UIView*)firstView
{

    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    [firstView.layer drawInContext:pdfContext];

    UIGraphicsEndPDFContext();

    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"resolution.pdf"];

    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

如何从这两个视图中绘制一个 PDF,因此 PDF 文件必须在一页包含 firstView 和 secondView

已添加

loadView 方法中的 ViewController 中

        AWFirstView *firstView = [[AWFirstView alloc] init];
    self.view = firstView;

    self.secondView = [[AWSecondtView alloc] initWithFrame:CGRectMake(0, 50, 300., 300.)];
    [self.secondView setBackgroundColor:[UIColor clearColor]];

     self.createPDFButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.createPDFButton.frame =CGRectMake(0, 0, 88., 50.);
    [self.createPDFButton setBackgroundColor:[UIColor whiteColor]];
    [self.createPDFButton addTarget:self action:@selector(createPDF) forControlEvents:UIControlEventTouchUpInside];
    [self updateView];
    [self.view addSubview:self.drawWindow];
    [self.view addSubview:self.createPDFButton];

在 updateView 中是这样的:

    -(void)updateView
{
    if(![self isViewLoaded])
        return;

    [[AWServices services].uiContext updateObjectInCurrentContext:&_resolution];


    AWFirstView *view = (AWFirstView *)self.view;
    view.post = _resolution.contact.officialPersonPost;
    view.name = _resolution.contact.officialPersonFullname;

    [view.superview setNeedsDisplay];

-(void)createPDF{
[self createPDFfromUIView:self.view];

}

【问题讨论】:

    标签: ios objective-c pdf uiview


    【解决方案1】:

    嗯,我加了

    [self.secondView.layer drawInContext:pdfContext];
    

    -(void)createPDFfromUIView 中,它起作用了。但这是正确的解决方案吗?

    【讨论】:

      【解决方案2】:

      试试这个:

      UIGraphicsBeginPDFContextToData(pdfData, firstView.bounds, nil);
      UIGraphicsBeginPDFPage();
      CGContextRef pdfContext = UIGraphicsGetCurrentContext();
      
      [firstView.layer drawInContext:pdfContext];
      

      请注意,我现在将drawInContext: 发送到firstView,而不是像您之前那样发送myView。如果将firstView 传递给方法,那么这应该会给出预期的结果。

      编辑:您没有将secondView 添加为子视图;这样做(第三行):

      self.secondView = [[AWSecondtView alloc] initWithFrame:CGRectMake(0, 50, 300., 300.)];
      [self.secondView setBackgroundColor:[UIColor clearColor]];
      [self.firstView addSubview:self.secondView];
      

      此外,您对self.view 的分配看起来不太好:

      self.view = firstView;
      

      你要么做:

      [self.view addSubview:self.firstView];
      

      或者您在视图控制器中定义 loadView 并将其分配给 self.view

      【讨论】:

      • 只保存firstView
      • 如果 secondView 是 firstView 的子视图,也应该保存在同一个 PDF 中。你能展示你如何创建 firstView、secondView 以及如何使后者成为前者的子视图吗?
      • 我已经添加了它,但它对我没有帮助。我传入@property 中的UIGraphicsBeginPDFContextToData(pdfData, self.firstView.bounds, nil); firstView 我也在[self.firstView.layer drawInContext:pdfContext]; 中传递它,所以我什至不再在-(void)createPDF{ [self createPDFfromUIView] 方法中传递它。如果我检查 self.firstView.subviews 它会将我的 secondView 显示为子视图
      猜你喜欢
      • 2019-01-22
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2013-02-28
      • 1970-01-01
      相关资源
      最近更新 更多