我有一个过程采用 HTML 字符串数组并使用以下方法逐页构建 PDF:
let printPageRenderer = ReportPrintPageRenderer(withTemplate: self.template)
var pageIndex = 0
data.forEach { (htmlPage) in
let printFormatter = UIMarkupTextPrintFormatter(markupText: htmlPage)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: pageIndex)
pageIndex += 1
}
let pdfData = drawPDFUsingPrintPageRendererNoImages(printPageRenderer: printPageRenderer)
drawPDFUsing... 方法使用了我多次描述过的标准方法:
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect.init(x: 0, y: 0, width: template.pageWidth, height: template.pageHeight), nil)
printPageRenderer.prepare(forDrawingPages: NSMakeRange(0, printPageRenderer.numberOfPages))
let bounds = UIGraphicsGetPDFContextBounds()
for i in 0...((printPageRenderer.numberOfPages - 1 < 0) ? 0 : printPageRenderer.numberOfPages - 1) {
UIGraphicsBeginPDFPage()
printPageRenderer.drawPage(at: i, in: bounds)
}
UIGraphicsEndPDFContext()
return data
整个过程非常完美,直到我找到大约 130 页长的 PDF 文档。在模拟器中,我可以毫无问题地打印 250 多页的文档。但是,对于实际设备(iPhone x 或 iPad pro)上的相同数据,data.forEach 循环一直运行到 pageIndex 达到 130 左右,然后它失败并在该行显示以下信息:
let printFormatter = UIMarkupTextPrintFormatter(markupText: htmlPage)
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1aa2b0330)
warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.
没有产生其他异常
内存使用率仍然可以,虽然它已经飙升,但考虑到我正在做的事情,它似乎并不过分。另外,在模拟器中运行时会这样增加,一般峰值只有300-400MB左右。
我尝试了以下方法来解决这个问题,但都没有成功:
我不知道这是怎么回事,也不知道如何解决这个问题。任何想法或帮助将不胜感激。
根据评论中的建议,这是在调试器中运行 bt 的跟踪:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1aa326a14)
frame #0: 0x00000001aa326a14 WebCore`bmalloc::IsoAllocator<bmalloc::IsoConfig<88u> >::allocateSlow(bool) + 252
frame #1: 0x00000001aaaf7f48 WebCore`WebCore::RenderText::createTextBox() + 28
frame #2: 0x00000001aab06e90 WebCore`WebCore::RenderTextLineBoxes::createAndAppendLineBox(WebCore::RenderText&) + 40
frame #3: 0x00000001aa9e2c50 WebCore`WebCore::RenderBlockFlow::constructLine(WebCore::BidiRunList<WebCore::BidiRun>&, WebCore::LineInfo const&) + 344
frame #4: 0x00000001aa9e5944 WebCore`WebCore::RenderBlockFlow::createLineBoxesFromBidiRuns(unsigned int, WebCore::BidiRunList<WebCore::BidiRun>&, WebCore::InlineIterator const&, WebCore::LineInfo&, WebCore::VerticalPositionCache&, WebCore::BidiRun*, WTF::Vector<WebCore::WordMeasurement, 64ul, WTF::CrashOnOverflow, 16ul>&) + 100
frame #5: 0x00000001aa9e825c WebCore`WebCore::RenderBlockFlow::layoutRunsAndFloatsInRange(WebCore::LineLayoutState&, WebCore::BidiResolverWithIsolate<WebCore::InlineIterator, WebCore::BidiRun, WebCore::BidiIsolatedRun>&, WebCore::InlineIterator const&, WebCore::BidiStatus const&, unsigned int) + 4576
frame #6: 0x00000001aa9e5e2c WebCore`WebCore::RenderBlockFlow::layoutRunsAndFloats(WebCore::LineLayoutState&, bool) + 920
frame #7: 0x00000001aa9ea03c WebCore`WebCore::RenderBlockFlow::layoutLineBoxes(bool, WebCore::LayoutUnit&, WebCore::LayoutUnit&) + 1800
frame #8: 0x00000001aa9cd5cc WebCore`WebCore::RenderBlockFlow::layoutBlock(bool, WebCore::LayoutUnit) + 1056
frame #9: 0x00000001aaadad84 WebCore`WebCore::RenderTableCell::layout() + 196
frame #10: 0x00000001aaae8794 WebCore`WebCore::RenderTableRow::layout() + 252
frame #11: 0x00000001aaaea914 WebCore`WebCore::RenderTableSection::layout() + 844
frame #12: 0x00000001aaacf744 WebCore`WebCore::RenderTable::layout() + 1916
frame #13: 0x00000001aa9cf578 WebCore`WebCore::RenderBlockFlow::layoutBlockChild(WebCore::RenderBox&, WebCore::RenderBlockFlow::MarginInfo&, WebCore::LayoutUnit&,
.....
如果我将它剥离回最基本的要素,甚至不尝试创建 PDF:
func exportHTMLContentToPDFNoImages(reportName name:String, fromHTMLData data: [String]) throws -> URL? {
data.shuffled().forEach { (htmlPage) in
autoreleasepool { () -> Void in
let _ = UIMarkupTextPrintFormatter(markupText: htmlPage)
}
}
return nil
}
它在第 130 次循环后仍然崩溃。用一些简单的 html 替换 htmlPage,比如
<html><body>Hi</body></html>
工作正常。所以我创建的表中一定有什么东西导致了这个问题。
更新
我能够将其简化为一个非常简单的示例,向我证明这是 iOS 中的一个错误。我也最终向苹果报告了它。幸运的是,这个错误似乎已在 13.1 中得到修复,因此即使 iOS 12 存在问题, future 仍有前进的道路。
最佳答案
要么内存不足,要么堆已损坏。我的猜测是,尽管您有内存使用情况屏幕截图,但您的内存已用完。
这个 EXC_BREAKPOINT 出现在 WebKit 的 bmalloc::IsoAllocator 中。
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1aa326a14)
frame #0: 0x00000001aa326a14 WebCore`bmalloc::IsoAllocator<bmalloc::IsoConfig<88u> >::allocateSlow(bool) + 252
frame #1: 0x00000001aaaf7f48 WebCore`WebCore::RenderText::createTextBox() + 28
Here is the line这引发了异常(注意这是来自 WebKit trunk,它可能与您手机上运行的版本不同):
template<typename Config>
BNO_INLINE void* IsoAllocator<Config>::allocateSlow(bool abortOnFailure)
{
...
return m_freeList.allocate<Config>([] () { BCRASH(); return nullptr; });
}
m_freeList.allocate 如果无法从它自己的列表分配,则运行 lambda(括号中的部分)中的内容。
BCRASH 引发您看到的异常。来自 BAssert.h :
// Crash with a SIGTRAP i.e EXC_BREAKPOINT.
// We are not using __builtin_trap because it is only guaranteed to abort, but not necessarily
// trigger a SIGTRAP. Instead, we use inline asm to ensure that we trigger the SIGTRAP.
#define BCRASH() do { \
BBreakpointTrap(); \
__builtin_unreachable(); \
} while (false)
此分配似乎是 WebKit 呈现您的 PDF 的一部分,但它可能会受到不断增长的 NSMutableData 的压力。
尝试使用 UIGraphicsBeginPDFContextToFile而不是 UIGraphicsBeginPDFContextToData 并查看崩溃是否仍然发生。 UIGraphicsBeginPDFContextToFile 的实现可能会将页面写入磁盘,而不是像您正在做的那样将整个结果一次性全部放入内存。
一旦切换到 UIGraphicsBeginPDFContextToFile,您就可以将 PDF 作为文件使用,或者,如果需要加载到 NSData 中,使用此方法使用内存映射文件:
let data = try Data(contentsOf: URL(fileURLWithPath: newPDFPath), options: .mappedIfSafe)
关于ios - 在 iOS 中从 HTML 创建 PDF 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57714080/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择