gpt4 book ai didi

ios - 在 Objective C 中将包含 UILabel、UIImageView 和 UITableView 的 UIView 保存为 PDF

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:32 25 4
gpt4 key购买 nike

我的 iOS 应用程序将选定的联系人详细信息保存为 PDF 并将其显示在 WebView 中。
我的 View 显示选定的联系人图像(在 UIImage 中)、姓名(在 UILabel 中)和其他详细信息,如电话号码(在 UITableView 中) ).
现在,我需要将此 View 保存为 PDF 并在另一个 View 中查看它。
我现在使用的方法 createPDFfromUIView 将我的 View 保存为 PDF。

但我无法在我的 PDF 中看到整个表格 View 。
我需要滚动才能看到整个表格 View 。

我该如何解决这个问题?

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

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];

// 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:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

最佳答案

成功了!

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename

{ CGRect priorBounds = self.contactDetails.bounds; CGSize fittedSize = [self.contactDetails sizeThatFits:CGSizeMake(priorBounds.size.width, self.contactDetails.contentSize.height)]; self.contactDetails.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height);

CGRect pdfPageBounds = CGRectMake(0, 30, 312, 800); // Change this as your need
NSMutableData *pdfData = [[NSMutableData alloc] init]; // Creates a mutable data object for updating with binary data, like a byte array

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor redColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), _detailView.bounds);

UIGraphicsBeginPDFContextToData(pdfData, _detailView.bounds, nil);
{

int page = 1;
for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height)
{

if(page == 1)
{
UIGraphicsBeginPDFPageWithInfo(_detailView.bounds, nil);

CGContextSaveGState(UIGraphicsGetCurrentContext());
{


[_contactImage.image drawInRect:CGRectMake( 200, 10, _contactImage.bounds.size.width, _contactImage.bounds.size.height)];
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont fontWithName:@"Arial" size:20],
NSStrokeWidthAttributeName : @(0),
NSStrokeColorAttributeName : [UIColor blackColor]
};
[_contactName.text drawAtPoint:CGPointMake(30 , 50) withAttributes:attributes];


CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 130);
[self.contactDetails.layer renderInContext:UIGraphicsGetCurrentContext()];


} CGContextRestoreGState(UIGraphicsGetCurrentContext());

}
else
{
UIGraphicsBeginPDFPageWithInfo(_detailView.bounds, nil);
CGContextSaveGState(UIGraphicsGetCurrentContext());
{
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
[self.contactDetails.layer renderInContext:UIGraphicsGetCurrentContext()];

} CGContextRestoreGState(UIGraphicsGetCurrentContext());
}
page++;
}

} UIGraphicsEndPDFContext();

self.contactDetails.bounds = priorBounds; // Reset the tableView


// Use the pdfData to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString* filePathPDF = [documentsPath stringByAppendingPathComponent:@"myPdf.pdf"]; //Add the file name
BOOL written = [pdfData writeToFile:filePathPDF atomically:YES];

关于ios - 在 Objective C 中将包含 UILabel、UIImageView 和 UITableView 的 UIView 保存为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894881/

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