Skip to content

Commit 495b138

Browse files
LiSiYuanLiSiYuan
LiSiYuan
authored and
LiSiYuan
committed
修复模拟器运行Epub格式文件路径问题
1 parent 6fe07ee commit 495b138

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

LSYReader/.DS_Store

2 KB
Binary file not shown.

LSYReader/LSYReader.xcodeproj/xcuserdata/LiSiYuan.xcuserdatad/xcschemes/xcschememanagement.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>AB9A96B61CFC2EE70066030C</key>
1616
<dict>
1717
<key>primary</key>
18-
<true/>
18+
<true />
1919
</dict>
2020
</dict>
2121
</dict>

LSYReader/LSYReader/LSYReader-Prefix.pch

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#define MinFontSize 11.0f
4343
#define MaxFontSize 20.0f
44+
#define kDocuments NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject
4445

4546
#endif
4647

LSYReader/LSYReader/Reader/Core/LSYReadUtilites.m

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "LSYChapterModel.h"
1111
#import "ZipArchive.h"
1212
#import "TouchXML.h"
13+
1314
@implementation LSYReadUtilites
1415
+(void)separateChapter:(NSMutableArray **)chapters content:(NSString *)content
1516
{
@@ -141,22 +142,23 @@ +(NSString *)unZip:(NSString *)path
141142
ZipArchive *zip = [[ZipArchive alloc] init];
142143
NSString *zipFile = [[path stringByDeletingPathExtension] lastPathComponent];
143144
if ([zip UnzipOpenFile:path]) {
144-
NSString *zipPath = [NSString stringWithFormat:@"%@/%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject,zipFile];
145+
NSString *zipPath = [NSString stringWithFormat:@"%@/%@",kDocuments,zipFile];
145146
NSFileManager *filemanager=[[NSFileManager alloc] init];
146147
if ([filemanager fileExistsAtPath:zipPath]) {
147148
NSError *error;
148149
[filemanager removeItemAtPath:zipPath error:&error];
149150
}
150151
if ([zip UnzipFileTo:[NSString stringWithFormat:@"%@/",zipPath] overWrite:YES]) {
151-
return zipPath;
152+
return zipFile;
152153
}
153154
}
154155
return nil;
155156
}
156157
#pragma mark - OPF文件路径
157158
+(NSString *)OPFPath:(NSString *)epubPath
158159
{
159-
NSString *containerPath = [NSString stringWithFormat:@"%@/META-INF/container.xml",epubPath];
160+
161+
NSString *containerPath = [NSString stringWithFormat:@"%@/%@/META-INF/container.xml",kDocuments,epubPath];
160162
//container.xml文件路径 通过container.xml获取到opf文件的路径
161163
NSFileManager *fileManager = [[NSFileManager alloc] init];
162164
if ([fileManager fileExistsAtPath:containerPath]) {
@@ -191,7 +193,8 @@ +(NSString *)OPFPath:(NSString *)epubPath
191193
#pragma mark - 解析OPF文件
192194
+(NSMutableArray *)parseOPF:(NSString *)opfPath
193195
{
194-
CXMLDocument* document = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:opfPath] options:0 error:nil];
196+
NSString *fullPath = [NSString stringWithFormat:@"%@/%@",kDocuments,opfPath];
197+
CXMLDocument* document = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fullPath] options:0 error:nil];
195198
NSArray* itemsArray = [document nodesForXPath:@"//opf:item" namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.idpf.org/2007/opf" forKey:@"opf"] error:nil];
196199
//opf文件的命名空间 xmlns="http://www.idpf.org/2007/opf" 需要取到某个节点设置命名空间的键为opf 用opf:节点来获取节点
197200
NSString *ncxFile;
@@ -204,7 +207,7 @@ +(NSMutableArray *)parseOPF:(NSString *)opfPath
204207
}
205208
}
206209

207-
NSString *absolutePath = [opfPath stringByDeletingLastPathComponent];
210+
NSString *absolutePath = [fullPath stringByDeletingLastPathComponent];
208211
CXMLDocument *ncxDoc = [[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", absolutePath,ncxFile]] options:0 error:nil];
209212
NSMutableDictionary* titleDictionary = [[NSMutableDictionary alloc] init];
210213
for (CXMLElement* element in itemsArray) {
@@ -236,7 +239,8 @@ +(NSMutableArray *)parseOPF:(NSString *)opfPath
236239
for (CXMLElement* element in itemRefsArray){
237240
NSString* chapHref = [itemDictionary objectForKey:[[element attributeForName:@"idref"] stringValue]];
238241
// LSYChapterModel *model = [LSYChapterModel chapterWithEpub:[NSString stringWithFormat:@"%@/%@",absolutePath,chapHref] title:[titleDictionary valueForKey:chapHref] imagePath:[opfPath stringByDeletingLastPathComponent]];
239-
LSYChapterModel *model = [LSYChapterModel chapterWithEpub:[NSString stringWithFormat:@"%@/%@",absolutePath,chapHref] title:[titleDictionary objectForKey:chapHref] imagePath:[[[opfPath stringByDeletingLastPathComponent]stringByAppendingPathComponent:chapHref] stringByDeletingLastPathComponent]];
242+
NSString *path = [[opfPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:chapHref];
243+
LSYChapterModel *model = [LSYChapterModel chapterWithEpub:path title:[titleDictionary objectForKey:chapHref] imagePath:[path stringByDeletingLastPathComponent]];
240244
[chapters addObject:model];
241245

242246
}

LSYReader/LSYReader/Reader/LSYReadPageViewController.m

+5-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ -(LSYMenuView *)menuView
9898
-(UIPageViewController *)pageViewController
9999
{
100100
if (!_pageViewController) {
101-
_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
101+
_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
102102
_pageViewController.delegate = self;
103103
_pageViewController.dataSource = self;
104104
[self.view addSubview:_pageViewController.view];
@@ -245,7 +245,8 @@ -(LSYReadViewController *)readViewWithChapter:(NSUInteger)chapter page:(NSUInteg
245245
[_model.record.chapterModel updateFont];
246246
if (_model.type == ReaderEpub) {
247247
if (!_model.chapters[chapter].epubframeRef) {
248-
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:_model.chapters[chapter].chapterpath]] encoding:NSUTF8StringEncoding];
248+
NSString *path = [kDocuments stringByAppendingPathComponent:_model.chapters[chapter].chapterpath];
249+
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path]] encoding:NSUTF8StringEncoding];
249250
_model.chapters[chapter].content = [html stringByConvertingHTMLToPlainText];
250251
[_model.chapters[chapter] parserEpubToDictionary];
251252
}
@@ -258,8 +259,8 @@ -(LSYReadViewController *)readViewWithChapter:(NSUInteger)chapter page:(NSUInteg
258259
if (_model.type == ReaderEpub) {
259260
_readView.type = ReaderEpub;
260261
if (!_model.chapters[chapter].epubframeRef) {
261-
NSLog(@"%@",_model.chapters[chapter].chapterpath);
262-
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:_model.chapters[chapter].chapterpath]] encoding:NSUTF8StringEncoding];
262+
NSString *path = [kDocuments stringByAppendingPathComponent:_model.chapters[chapter].chapterpath];
263+
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path]] encoding:NSUTF8StringEncoding];
263264
_model.chapters[chapter].content = [html stringByConvertingHTMLToPlainText];
264265
[_model.chapters[chapter] parserEpubToDictionary];
265266
[_model.chapters[chapter] paginateEpubWithBounds:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width-LeftSpacing-RightSpacing, [UIScreen mainScreen].bounds.size.height-TopSpacing-BottomSpacing)];

LSYReader/LSYReader/Reader/Model/LSYChapterModel.m

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "LSYReadParser.h"
1212
#import "NSString+HTML.h"
1313
//#include <vector>
14+
1415
@interface LSYChapterModel ()
1516
@property (nonatomic,strong) NSMutableArray *pageArray;
1617
@end
@@ -24,14 +25,23 @@ - (instancetype)init
2425
}
2526
return self;
2627
}
28+
//-(NSString *)chapterpath {
29+
// NSLog(@"----------------------%@",_chapterpath);
30+
// return [kDocuments stringByAppendingPathComponent:_chapterpath];
31+
//}
32+
//-(NSString *)epubImagePath {
33+
// return [kDocuments stringByAppendingPathComponent:_epubImagePath];
34+
//}
2735
+(id)chapterWithEpub:(NSString *)chapterpath title:(NSString *)title imagePath:(NSString *)path
2836
{
2937
LSYChapterModel *model = [[LSYChapterModel alloc] init];
38+
3039
model.title = title;
3140
model.epubImagePath = path;
3241
model.type = ReaderEpub;
3342
model.chapterpath = chapterpath;
34-
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:chapterpath]] encoding:NSUTF8StringEncoding];
43+
NSString * fullPath= [kDocuments stringByAppendingPathComponent:chapterpath];
44+
NSString* html = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:fullPath]] encoding:NSUTF8StringEncoding];
3545
model.html = html;
3646
model.content = [html stringByConvertingHTMLToPlainText];
3747
[model parserEpubToDictionary];
@@ -49,7 +59,7 @@ -(void)parserEpubToDictionary
4959
if ([scanner scanString:@"<img>" intoString:NULL]) {
5060
NSString *img;
5161
[scanner scanUpToString:@"</img>" intoString:&img];
52-
NSString *imageString = [self.epubImagePath stringByAppendingPathComponent:img];
62+
NSString *imageString = [[kDocuments stringByAppendingPathComponent:self.epubImagePath] stringByAppendingPathComponent:img];
5363
UIImage *image = [UIImage imageWithContentsOfFile:imageString];
5464

5565
CGFloat width = ScreenSize.width - LeftSpacing - RightSpacing;

0 commit comments

Comments
 (0)