- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个名为 UIView+PDFView 的 UIView 扩展类,它采用当前 View ,将其拆分为页面,并呈现 PDF 文档。我的问题在于渲染部分;我可以成功地为内容“创建”页面,但是第一页之后的所有页面都是空白的。我的最终目标是采用当前 View ,“缩放”等于页面的宽度,并在 PDF 中对 View 的其余部分进行分页。
问题不在于:
我的代码如下:
@interface UIView (PDFView)
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo;
@end
@implementation UIView (PDFView)
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo {
// http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html#//apple_ref/doc/constant_group/Auxiliary_Dictionary_Keys
// 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
CGSize pageSize = CGSizeMake(self.bounds.size.width, 792);
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, documentInfo);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
NSInteger currentPage = 0;
BOOL done = NO;
do {
CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
UIGraphicsBeginPDFPageWithInfo(currentPageRect, nil);
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[self.layer renderInContext:pdfContext];
// If we're at the end of the view, exit the loop.
if ((pageSize.height*currentPage) > self.bounds.size.height) {
done = YES;
} else {
currentPage++;
}
} while (!done);
// remove PDF rendering context
UIGraphicsEndPDFContext();
if (aFilename == nil) {
return pdfData;
} else {
// 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);
return nil;
}
}
最佳答案
我觉得你的问题出在
CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
而不是尝试使用以下任一语句
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);
UIGraphicsBeginPDFPage();
每次您希望向上下文添加新页面时,使用上述语句,您就可以向上下文添加页面。
如果您想使用页面的默认大小,即 612 X 792,您可以直接使用 UIGraphicsBeginPDFPage();
对于自定义页面大小,您可以使用 UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);
我认为这应该可以解决您的问题。
关于objective-c - Obj-C View to PDF 只呈现第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10453733/
SCENARIO 有两页,第一页是HomePage,它在flutter_bloc软件包的帮助下自动获取api数据。在首页(第一页)中,还有一个按钮,可在此代码Navigator.push(contex
我检查过类似的问题,但由其他人发布,但我仍然看不到我的代码有什么问题。 我刚刚从文档中复制了它 - https://symfony.com/doc/3.4/page_creation.html Luc
我已经编写了一段代码,使用Python中的Scrapy来抓取页面。下面我粘贴了 main.py 代码。但是,每当我运行我的蜘蛛时,它仅从第一页抓取(DEBUG:从抓取),这也是请求中的Referer标
我创建了一个 ios 图书阅读器应用程序。在这个应用程序中,我集成了 google drive 和 skydrive 。现在我可以从 google drive 和 skydrive 登录和检索数据了。
效果图: 功能简介:可使用上下键选中行,选中后点击修改,textbox获得gridview中的代码的数据。对你有帮助的话,请记得要点击“好文要顶”哦!!!不懂的,请留言。废话不多说了,贴码如下
我是一名优秀的程序员,十分优秀!