2014-06-23 54 views
0

我有第一個視圖firstView,也畫第二個視圖secondView並將其作爲子視圖添加到firstView。 我也有從firstView PDF文件創建方法:從兩個視圖中繪製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

新增

在的ViewController在loadView方法

 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]; 

在更新視圖是這樣的:

-(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]; 

}

回答

0

嗯,我在-(void)createPDFfromUIView添加

[self.secondView.layer drawInContext:pdfContext]; 

和它的工作。但這是正確的解決方案嗎?

0

試試這個:

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

+0

它只保存firstView – dav

+0

如果secondView是firstView的子視圖,它也應該保存在同一PDF中。你能告訴你如何創建firstView,secondView以及如何讓後者成爲前者的子視圖? – sergio

+0

增加了更多代碼 – dav