ios 阅读 ePub 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1388467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading ePub format
提问by MobX
I am trying to develop an iPhone application to read ePub files. Is there any framework available to develop this? I have no idea about how to read this file format. I tried to parse a sample file with .epub extension using NSXML Parser, but that fails.
我正在尝试开发一个 iPhone 应用程序来读取 ePub 文件。有没有可用的框架来开发这个?我不知道如何阅读这种文件格式。我尝试使用 NSXML Parser 解析扩展名为 .epub 的示例文件,但失败了。
回答by Euan
The EPUB format brings together a bunch of different specifications / formats:
EPUB 格式汇集了一系列不同的规范/格式:
- one to say what the content of the book should look like (a subset of XHTML 1.1 + CSS)
- one to define a "manifest" that lists all of the files that make up that content (OPF, which is an XML file)
- one to define how everything is packaged up (OEBPS: a zip file of everything in the manifest plus a few extra files)
- 一个说书的内容应该是什么样子的(XHTML 1.1 + CSS的一个子集)
- 一个定义一个“清单”,列出构成该内容的所有文件(OPF,这是一个 XML 文件)
- 一个定义如何打包所有内容(OEBPS:清单中所有内容的 zip 文件以及一些额外文件)
The specs look a bit daunting but actually once you've got the basics (unzipping, parsing XML) down it's not particularly difficult or complex.
规范看起来有点令人生畏,但实际上,一旦您掌握了基础知识(解压缩、解析 XML),它就不是特别困难或复杂。
You'll need to work out how to download the EPUB, to unzip it somewhere, to parse the manifest and then to display the relevant content.
您需要弄清楚如何下载 EPUB、将其解压缩到某处、解析清单,然后显示相关内容。
Some pointers if you're just starting out:
如果您刚刚开始,请提供一些建议:
To display content just use a UIWebView
for now.
要显示内容,请暂时使用 a UIWebView
。
Here's a high level step by step for your code:
这是您的代码的高级分步说明:
1) create a view with a UIWebView
1)创建一个视图 UIWebView
2) download the EPUB file
2)下载EPUB文件
3) unzip it to a subdirectory in your app's documents folder using the zip library, linked above
3) 使用上面链接的 zip 库将其解压缩到应用程序文档文件夹中的子目录中
4) parse the XML file at META-INF/container.xml
(if this file doesn't exist the EPUB is invalid) using TBXML, linked above
4)META-INF/container.xml
使用上面链接的 TBXML解析 XML 文件(如果此文件不存在,则 EPUB 无效)
5) In this XML, find the first "rootfile" with media-type application/oebps-package+xml
. This is the OPF file for the book.
5) 在此 XML 中,找到具有 media-type 的第一个“根文件” application/oebps-package+xml
。这是本书的 OPF 文件。
6) parse the OPF file (also XML)
6)解析OPF文件(也是XML)
7) now you need to know what the first chapter of the book is.
7) 现在你需要知道这本书的第一章是什么。
a) each <item>
in the <manifest>
element has an id and an href. Store these in an NSDictionary
where the key is the id and the object is the href.
一个),每个<item>
在<manifest>
元件具有ID和一个href。将这些存储在NSDictionary
键是 id 并且对象是 href 的地方。
b) Look at the first <itemref>
in the <spine>
. It has an idref attribute which corresponds to one of the ids in (a). Look up that id in the NSDictionary
and you'll get an href.
B)先看第一个<itemref>
在<spine>
。它有一个 idref 属性,对应于 (a) 中的 id 之一。在 中查找该 ID NSDictionary
,您将获得一个 href。
c) this is the the file of the first chapter to show the user. Work out what the full path is (hint: it's wherever you unzipped the zip file to in (3) plus the base directory of the OPF file in (6))
c) 这是第一章的文件给用户看。找出完整路径是什么(提示:它是您将 zip 文件解压缩到 (3) 中的任何位置以及 (6) 中 OPF 文件的基本目录)
8) create an NSURL
using fileURLWithPath:
, where the path is the full path from (7c). Load this request using the UIWebView
you created in (1).
8) 创建一个NSURL
using fileURLWithPath:
,其中路径是来自 (7c) 的完整路径。使用UIWebView
您在 (1) 中创建的加载此请求。
You'll need to implement forward / backward buttons or swipes or something so that users can move from one chapter to another. Use the <spine>
to work out which file to show next - the <itemrefs>
in the XML are in the order they should appear to the reader.
您需要实现前进/后退按钮或滑动或其他东西,以便用户可以从一章移动到另一章。使用<spine>
来确定接下来要显示的文件 - <itemrefs>
XML 中的 是按照它们应该向读者显示的顺序。
回答by p4bl0
Apparently EPUB is "just" an XML format, so if you have an xml parser and the specit should be okay.
显然 EPUB “只是”一种 XML 格式,所以如果你有一个 xml 解析器和规范,它应该没问题。
Plus a little tuto? Have fun!
加上一点补习?玩得开心!
EDIT: you could also read some code here, this is for generating epub, not reading them but the code may be useful.
编辑:您也可以在这里阅读一些代码,这是用于生成 epub,而不是阅读它们,但代码可能有用。
EDIT again: And see links to related question in the right sidebar, there are some links in the answers to free ebook reader which support ePub.
再次编辑:并在右侧边栏中查看相关问题的链接,在支持 ePub 的免费电子书阅读器的答案中有一些链接。
EDIT 3: You should add a comment when you edit your question so people who answer you can continue the discussion (if you don't comment we're not noticed of your edit).
编辑 3:您应该在编辑问题时添加评论,以便回答您的人可以继续讨论(如果您不发表评论,我们不会注意到您的编辑)。
So, The parsing fail because you didn't read the spec or related questions on Stack Overflow... *.epub file are a zipped folder containing XML file(s), not plain xml.
因此,解析失败是因为您没有阅读 Stack Overflow 上的规范或相关问题... *.epub 文件是一个包含 XML 文件的压缩文件夹,而不是普通的 xml。
回答by yonkeltron
I read through this tutorial once (free registration required, sorry) and it gave me a great introduction to ePub. deverloperWorks tutorial here
我通读了一次本教程(需要免费注册,抱歉),它为我提供了对 ePub 的一个很好的介绍。devloperWorks教程在这里
I highly suggest you look at some of the XML processing libraries. If you just want to get specific information out of the XML file, then you can pick the right parsing strategy.
我强烈建议您查看一些 XML 处理库。如果您只想从 XML 文件中获取特定信息,那么您可以选择正确的解析策略。
回答by camino
there is an open source project fbreader,
有一个开源项目 fbreader,
it also support iphone
它还支持iphone
回答by camino
I'm playing arround to create an epub-framework for iphone apps.
我正在为 iphone 应用程序创建一个 epub 框架。
At the moment (I really just startet) i can generate a title page with links to the chapters.
目前(我真的只是开始)我可以生成一个带有章节链接的标题页。
My approach is
我的方法是
- Use quickconnect iphone framework as a layer (maybe i change to phonegap) which basically allows for javascript apps as iphone apps
- Add the UNZIPed epub as a ressource to the project
- Parse the whole thing with a customized version of the epub.js (somewhere on google-code)
- 使用 quickconnect iphone 框架作为一个层(也许我改成 phonegap),它基本上允许 javascript 应用程序作为 iphone 应用程序
- 将解压后的 epub 作为资源添加到项目中
- 使用 epub.js 的自定义版本(在 google-code 上的某处)解析整个内容
Right now I'm looking into pageflip, some kind of gui and minor usability issues (save the current page beingviewed)
现在我正在研究 pageflip、某种 gui 和较小的可用性问题(保存正在查看的当前页面)
I hope that give's you an idea on how to start
我希望这能让你知道如何开始
回答by Dwight Kelly
Jonathan Wight (schwa) has developed a ObjC solution for parsing and displaying ePub documents on the iPhone. It's part of his TouchCode open source repository.
Jonathan Wight (schwa) 开发了一个 ObjC 解决方案,用于在 iPhone 上解析和显示 ePub 文档。它是他的 TouchCode 开源存储库的一部分。