gpt4 book ai didi

ios - 从 UIScrollView Objective C 创建 PDF

转载 作者:可可西里 更新时间:2023-11-01 06:24:38 26 4
gpt4 key购买 nike

此问题之前已发布(Creating PDF from UIScrollView in iphone app),我使用的代码来自here .

这是代码

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// 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
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

[aView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

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

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

设置:我有一个 UIScrollView,里面是一个 UIView。我想保存整个 UIView (950,500px),它位于 (520,500) 的空间(UIScrollView 大小)中。生成的PDF只有UIScrollView的大小(520,500)

我读了answer但他显然更改了代码,但它对我不起作用。我整天都在努力解决这个问题。

我是初学者,所以请指出我应该在我的问题中添加我遗漏的任何内容。谢谢。

PS - 这是一款 iPad 应用。

最佳答案

上下文的大小应该是 ScrollView 的内容大小,而不是边界。

然后您需要临时将 ScrollView 的大小调整为其内容大小,在 PDF 上下文中呈现它,并将 ScrollView 的大小恢复为其原始大小。

UIGraphicsBeginPDFContextToData(pdfData, (CGRect){0,0, scrollView.contentSize}, nil);

CGRect origSize = scrollView.frame;
CGRect newSize = origSize;
newSize.size = scrollView.contentSize;
[scrollView setFrame:newSize];

[scrollView.layer renderInContext:pdfContext];

[scrollView setFrame:origSize];

关于ios - 从 UIScrollView Objective C 创建 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21562085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com