2012-04-13 51 views
1

我知道如何保存uiview在PDF中,但我的問題是如何保存在PDF中的整個uiview,例如,如果你有一個頁面,它比iPad顯示更長,你需要滾動它只能將活動視圖保存爲PDF文件,但其餘部分不存在。 我正在尋找幫助或任何幫助我的教程。保存整個uiview在PDF

在此先感謝。

回答

1

可以渲染這樣的觀點爲PDF:

#define kDefaultPageHeight 1024 
#define kDefaultPageWidth 768 
#define kMargin 5 

CGRect origframe = someView.frame; 
NSString *heightStr = origFrame.size.height; 
int height = [heightStr intValue]; 

// Size of the view in the pdf page 
CGFloat maxHeight = kDefaultPageHeight - 2*kMargin; 
CGFloat maxWidth = kDefaultPageWidth - 2*kMargin; 
int pages = ceil(height/maxHeight); // or use 1 if you only have 1 page. 

UIGraphicsBeginPDFContextToFile(@"some/full/path.pdf", CGRectZero, nil); 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(currentContext, kMargin, kMargin); 

/* this is the view that you will render to the PDF */ 
[someView.layer renderInContext:currentContext]; 

UIGraphicsEndPDFContext(); 

當然,這是代碼的一個粗略的片段,則需要調整,以適應您的需求

+1

爲了完整起見,而'renderInContext'是最好的答案,如果你使用'CALayer'的某些特性,它可能不起作用。請參閱[無法捕獲視圖圖層中的遮罩](http://stackoverflow.com/q/9615235/1318452)。 – Dondragmer 2012-04-14 01:32:24

+0

讓我試試看,我們會看到,但請告訴我,這段代碼是否可以保存更長的頁面? – user1021736 2012-04-16 19:21:06

+0

還有一件事,在這段代碼中,您使用CGRect繪製第四幀並查看如果使用故事板會發生什麼?如何實現它?以及如何將其他視圖添加到此PDF中作爲其他頁面。 – user1021736 2012-04-16 19:28:15