-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathRNPDFView.m
More file actions
136 lines (115 loc) · 3.31 KB
/
Copy pathRNPDFView.m
File metadata and controls
136 lines (115 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// RNPDFView.m
// App
//
// Created by Sibelius Seraphini on 3/1/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
#import <React/RCTLog.h>
#import "TiledPDFView.h"
#import "PDFScrollView.h"
#import "RNPDFView.h"
@implementation RNPDFView {
UILabel *_titleLabel;
CGPDFDocumentRef _pdf;
int _numberOfPages;
CGPDFPageRef _page;
CGFloat _PDFScale;
TiledPDFView *_tiledPDFView;
PDFScrollView *_pdfScrollView;
}
- (instancetype)init
{
if ((self = [super init])) {
_tiledPDFView = [[TiledPDFView alloc] initWithFrame:self.bounds scale:_PDFScale];
}
return self;
}
#pragma mark - React View Management
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
{
RCTLogError(@"image cannot have any subviews");
return;
}
- (void)removeReactSubview:(UIView *)subview
{
RCTLogError(@"image cannot have any subviews");
return;
}
- (void)reloadPdf
{
if (self.path == (id)[NSNull null] || self.path.length == 0) {
NSLog(@"null path");
} else {
NSLog(@"not null: %@", self.path);
NSURL *pdfURL = [NSURL fileURLWithPath:self.path];
_pdf = CGPDFDocumentCreateWithURL( (__bridge CFURLRef) pdfURL );
_numberOfPages = (int)CGPDFDocumentGetNumberOfPages( _pdf );
if (self.pageNumber != nil && [self.pageNumber intValue] <= _numberOfPages) {
_page = CGPDFDocumentGetPage( _pdf, [self.pageNumber unsignedIntValue] );
} else {
_page = CGPDFDocumentGetPage( _pdf, 1 );
}
NSLog(@"self.page==NULL? %@",_page==NULL?@"yes":@"no");
_pdfScrollView = [[PDFScrollView alloc] initWithFrame:self.bounds];
_pdfScrollView.PDFScale = 1;
[_pdfScrollView setPDFPage:_page];
[self addSubview:_pdfScrollView];
}
}
- (void)setPageNumber:(NSNumber *)pageNumber
{
if (![pageNumber isEqual:_pageNumber]) {
NSLog(@"setPageNumber %@ -> %@", _pageNumber, pageNumber);
_pageNumber = [pageNumber copy];
[self reloadPdf];
}
}
- (void)setSrc:(NSString *)src
{
if (![src isEqual:_path]) {
_path = [src copy];
[self reloadPdf];
}
}
- (void)setPath:(NSString *)path
{
if (![path isEqual:_path]) {
_path = [path copy];
[self reloadPdf];
}
}
- (void)setZoom:(NSNumber *)zoom
{
if (![zoom isEqual:_zoom]) {
NSLog(@"setZoom %@ -> %@", _zoom, zoom);
_zoom = [zoom copy];
[self reloadPdf];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect pageRect = CGPDFPageGetBoxRect( _page, kCGPDFMediaBox );
CGFloat yScale = self.bounds.size.height/pageRect.size.height;
CGFloat xScale = self.bounds.size.width/pageRect.size.width;
CGFloat myScale = MIN( xScale, yScale );
NSLog(@"%s self.myScale=%f",__PRETTY_FUNCTION__, myScale);
_pdfScrollView.frame = self.bounds;
_pdfScrollView.zoomScale = (_zoom == NULL ? 1.0 : [_zoom doubleValue]);
_pdfScrollView.PDFScale = myScale;
_pdfScrollView.tiledPDFView.bounds = self.bounds;
_pdfScrollView.tiledPDFView.myScale = myScale;
[_pdfScrollView.tiledPDFView.layer setNeedsDisplay];
NSLog(@"onChange==NULL? %@",_onChange==NULL?@"yes":@"no");
if(_onChange){
NSLog(@"onChange %d", _numberOfPages);
_onChange(@{ @"message": @(_numberOfPages) });
}
}
@end