xcode 如何为 iPad/iPhone 应用程序实现 ePub 阅读器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12242499/
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
How to implement an ePub reader for an iPad/iPhone app
提问by Kanak Vaghela
I want to implement an ePub reader for the iOS platform. Please suggest any open source code for book flipping animation, bookmarks, font-size customization and single page view (without scroll bars).
我想为 iOS 平台实现一个 ePub 阅读器。请建议任何用于翻书动画、书签、字体大小自定义和单页视图(无滚动条)的开源代码。
回答by tyr.kassat
As the previous article points out, there is no API that given an ePub will just display it -- you need to do some work:
正如前一篇文章所指出的,没有任何 API 可以让 ePub 只显示它——您需要做一些工作:
- Unzip the ePub
- Read the manifest file and metadata file to find the xhtml documents to display
- Load the xhtml documents into a UIWebView using a file:/// URL to the unzipped document
- 解压 ePub
- 读取清单文件和元数据文件,找到要显示的 xhtml 文档
- 使用 file:/// 解压文档的 URL 将 xhtml 文档加载到 UIWebView
If you want to ensure that the documents don't hit the network you'll need to implement a custom NSURLProtocol and serve the bytes for the files yourself as file:/// allows cross domain access.
如果您想确保文档不会访问网络,您需要实现自定义 NSURLProtocol 并自己为文件提供字节,因为 file:/// 允许跨域访问。
That will display the content just fine, but the "hard" part is moving between the documents (which usually represent a whole chapter). This is the work that iBooks and other apps do for you.
这将很好地显示内容,但“困难”部分是在文档之间移动(通常代表一整章)。这是 iBooks 和其他应用程序为您完成的工作。
NOTE: For the UIWebView to display the content correctly, you have to ensure that the file has a .xhtml extension when using file:/// urls. If you implement your own URL protocol handler, you need to make sure the protocol handler returns the correct xml content type for xhtml, namely:
注意:要使 UIWebView 正确显示内容,您必须确保在使用 file:/// url 时文件具有 .xhtml 扩展名。如果您实现自己的 URL 协议处理程序,则需要确保协议处理程序为 xhtml 返回正确的 xml 内容类型,即:
application/xhtml+xml
应用程序/xhtml+xml
回答by Paul Sweatte
Use the ePub packaging format and an open-source reader for reference:
使用 ePub 打包格式和开源阅读器作为参考:
回答by Rajesh Loganathan
Try this steps :
试试这个步骤:
Source code:AePubReader
源代码:AePubReader
Implementation:
执行:
Step 1:Create a view with a UIWebView
第 1 步:创建一个视图UIWebView
Step 2:Download an EPUBfile and import into your project
第 2 步:下载EPUB文件并导入到您的项目中
Step 3:Unzip EPUBfile to a subdirectory in your app's documents folder.
第 3 步:将EPUB文件解压缩到应用程序文档文件夹中的子目录中。
Step 4:Parse the XML
file from directory META-INF/container.xml
. If this file directory doesn't exist means, your EPUB file is invalid.
第 4 步:解析XML
目录中的文件META-INF/container.xml
。如果此文件目录不存在,则表示您的 EPUB 文件无效。
Step 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
,找到第一个rootfile
带有 media-type 的“ ” application/oebps-package+xml
。这是本书的 OPF 文件。
Step 6:Parse the OPF file (also XML)
第 6 步:解析 OPF 文件(也是 XML)
Step 7:Now you need to know what the first chapter of the book is.
第 7 步:现在您需要知道本书的第一章是什么。
- Each in the element has an id and an href. Store these in an
NSDictionary
where the key is the id and the object is the href. - Look at the first in the . It has an idref attribute which corresponds to one of the ids in (Step 7a). Look up that id in the
NSDictionary
and you'll get an href. - 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 (Step 3), plus the base
NSDictionary
of the OPF file in (Step 6).
- 元素中的每个元素都有一个 id 和一个 href。将这些存储在
NSDictionary
键是 id 并且对象是 href 的地方。 - 看第一个。它有一个 idref 属性,它对应于(步骤 7a)中的 id 之一。在 中查找该 ID
NSDictionary
,您将获得一个 href。 - 这是第一章向用户展示的文件。计算出完整路径是什么(提示:它是您将 zip 文件解压缩到(步骤 3)中的任何位置,以及
NSDictionary
(步骤 6)中的 OPF 文件的基础。
Step 8:Create an NSURL
using fileURLWithPath:, where the path is the full path from (Step 7c). Load this request using the UIWebView you created in (Step 1).
步骤 8:创建一个NSURL
using fileURLWithPath:,其中路径是来自(步骤 7c)的完整路径。使用您在(步骤 1)中创建的 UIWebView 加载此请求。
Step 9:You'll need to implement forward / backward
buttons or swipes
or something so that users can move from one chapter to another. Use the to work out which file to show next. Then the XML
are in the order they should appear to the reader.
第 9 步:您需要实现forward / backward
按钮或swipes
其他东西,以便用户可以从一章移动到另一章。使用 确定下一个要显示的文件。然后XML
是按照它们应该出现在读者面前的顺序。
These step got referred from this link
这些步骤是从此链接引用的
回答by Karan Alangat
Use UITextView with pageview controller . (Specify your doubts , if any)
将 UITextView 与页面视图控制器一起使用。(如果有的话,请说明您的疑问)