textView 框架的更改如何使其在 pdf 导出后无法选择文本?它是简单的光栅化与可怕的压缩。如果我导出 textView 并且不更改框架,我会得到完美的向量。
我用 textView 查看,然后用 textView 的内容导出 pdf,如下所示:
- (void) generatePDFWithFilename:(NSString *)filename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// pdf view
_pdfView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 612, 792)];
// adding subview - for easier positioning
CGRect tempFrame;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, _pdfView.bounds.size.width - 20, _pdfView.bounds.size.height - 20)];
tempView.backgroundColor = [UIColor redColor];
_pdfView.backgroundColor = [UIColor lightGrayColor];
[_pdfView addSubview:tempView];
UITextView *pdfDescriptionTextView = [[UITextView alloc] initWithFrame:CGRectZero];
pdfDescriptionTextView = _workDescriptionTextView;
[tempView addSubview:pdfDescriptionTextView];
// pdfDescriptionTextView.center = CGPointMake(50, 200); // working - vector data are preserved
tempFrame = pdfDescriptionTextView.frame;
tempFrame.origin = CGPointMake(100, 100);
tempFrame.size = CGSizeMake(tempView.bounds.size.width - 40, pdfDescriptionTextView.contentSize.height);
pdfDescriptionTextView.frame = tempFrame;
pdfDescriptionTextView.backgroundColor = [UIColor greenColor];
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(_pdfView.bounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[_pdfView.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];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:filename];
// instructs the mutable data object to write its context to a file on disk
BOOL success = [pdfData writeToFile:documentDirectoryFilename atomically:YES];
if(success)
{
[self sendEmailWithData:pdfData];
}
}