PDFPageEditCell.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // PDFPageEditCell.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFPageEditCell.h"
  11. @implementation PDFEditPage
  12. - (id)initWithPageRef:(CGPDFPageRef)pageRef {
  13. if (self = [super init]) {
  14. if (pageRef) {
  15. _pageRef = CGPDFPageRetain(pageRef);
  16. _size = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox).size;
  17. } else {
  18. _size = CGSizeMake(595, 842);
  19. }
  20. }
  21. return self;
  22. }
  23. - (id)init {
  24. if (self = [super init]) {
  25. _size = CGSizeMake(595, 842);
  26. }
  27. return self;
  28. }
  29. - (void)dealloc {
  30. if (_pageRef) {
  31. CGPDFPageRelease(_pageRef);
  32. }
  33. [_imagePath release];
  34. [super dealloc];
  35. }
  36. - (CGSize)sizeThatFits:(CGSize)size {
  37. CGPDFPageRef pageRef = self.pageRef;
  38. CGRect boxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
  39. CGAffineTransform transform = CGPDFPageGetDrawingTransform(pageRef,
  40. kCGPDFCropBox,
  41. CGRectMake(0, 0, size.width, size.height),
  42. 0,
  43. true);
  44. CGRect rect = CGRectApplyAffineTransform(boxRect, transform);
  45. return rect.size;
  46. }
  47. @end
  48. @interface PDFPageView : UIView
  49. @property (nonatomic,assign) CGPDFPageRef pageRef;
  50. @end
  51. @implementation PDFPageView
  52. - (instancetype)initWithFrame:(CGRect)frame {
  53. if (self = [super initWithFrame:frame]) {
  54. CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
  55. tiledLayer.levelsOfDetail = 4;
  56. tiledLayer.levelsOfDetailBias = 4;
  57. tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
  58. }
  59. return self;
  60. }
  61. - (void)dealloc {
  62. CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
  63. tiledLayer.contents = nil;
  64. tiledLayer.delegate = nil;
  65. [tiledLayer removeFromSuperlayer];
  66. CGPDFPageRelease(_pageRef);
  67. [super dealloc];
  68. }
  69. + (Class)layerClass {
  70. return [CATiledLayer class];
  71. }
  72. + (CFTimeInterval)fadeDuration {
  73. return 0.0;
  74. }
  75. - (void)setPageRef:(CGPDFPageRef)pageRef {
  76. if (_pageRef != pageRef) {
  77. CGPDFPageRelease(_pageRef);
  78. _pageRef = CGPDFPageRetain(pageRef);
  79. }
  80. }
  81. - (void)drawRect:(CGRect)rect {
  82. CGContextRef context = UIGraphicsGetCurrentContext();
  83. CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
  84. CGContextFillRect(context, CGContextGetClipBoundingBox(context));
  85. CGContextSaveGState(context);
  86. if (!self.pageRef) {
  87. return;
  88. }
  89. CGAffineTransform transform = CGPDFPageGetDrawingTransform(self.pageRef,
  90. kCGPDFCropBox,
  91. self.bounds,
  92. 0,
  93. true);
  94. CGContextSaveGState(context);
  95. CGContextTranslateCTM(context, 0, rect.size.height);
  96. CGContextScaleCTM(context, 1, -1);
  97. CGContextConcatCTM(context, transform);
  98. CGContextSetInterpolationQuality(context, kCGInterpolationLow);
  99. CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
  100. CGContextSetShouldAntialias(context, NO);
  101. CGContextSetShouldSmoothFonts(context, NO);
  102. CGContextDrawPDFPage(context, self.pageRef);
  103. CGContextRestoreGState(context);
  104. }
  105. @end
  106. @interface PDFPageEditCell ()
  107. @property (nonatomic,retain) UIImageView *pageView;
  108. @property (nonatomic,assign) CGRect displayBounds;
  109. @end
  110. @implementation PDFPageEditCell
  111. - (instancetype)initWithFrame:(CGRect)frame {
  112. if (self = [super initWithFrame:frame]) {
  113. _displayBounds = CGRectMake(0, 0, frame.size.width, MIN(frame.size.width, frame.size.height));
  114. _pageView = [[UIImageView alloc] initWithFrame:self.bounds];
  115. _pageView.backgroundColor = [UIColor whiteColor];
  116. [self.contentView addSubview:_pageView];
  117. _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_displayBounds),
  118. frame.size.width,
  119. frame.size.height-CGRectGetMaxY(_displayBounds))];
  120. _textLabel.textAlignment = NSTextAlignmentCenter;
  121. _textLabel.font = [UIFont systemFontOfSize:12.0];
  122. [self.contentView addSubview:_textLabel];
  123. }
  124. return self;
  125. }
  126. - (void)dealloc {
  127. [_page release];
  128. [_pageView release];
  129. [_textLabel release];
  130. [super dealloc];
  131. }
  132. - (void)setPage:(PDFEditPage *)page {
  133. NSTimeInterval duration = 0.3;
  134. if (_page != page) {
  135. [_page release];
  136. _page = [page retain];
  137. for (UIView *view in self.pageView.subviews) {
  138. [view removeFromSuperview];
  139. }
  140. if (self.page.pageRef) {
  141. PDFPageView *pdfPageView = [[PDFPageView alloc] initWithFrame:self.pageView.bounds];
  142. pdfPageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  143. pdfPageView.pageRef = page.pageRef;
  144. [self.pageView addSubview:pdfPageView];
  145. [pdfPageView release];
  146. }
  147. duration = 0.0;
  148. }
  149. self.pageView.image = nil;
  150. [UIView animateWithDuration:duration animations:^{
  151. CGRect boxRect;
  152. if (self.page.pageRef) {
  153. boxRect = CGPDFPageGetBoxRect(self.page.pageRef, kCGPDFCropBox);
  154. CGAffineTransform transform = CGPDFPageGetDrawingTransform(self.page.pageRef,
  155. kCGPDFCropBox,
  156. self.displayBounds,
  157. 0,
  158. true);
  159. boxRect = CGRectApplyAffineTransform(boxRect, transform);
  160. } else {
  161. if (self.page.imagePath) {
  162. self.pageView.image = [UIImage imageWithContentsOfFile:self.page.imagePath];
  163. }
  164. boxRect = CGRectMake(0, 0, self.page.size.width, self.page.size.height);
  165. if (boxRect.size.width < boxRect.size.height) {
  166. CGFloat width = boxRect.size.width*self.displayBounds.size.height/boxRect.size.height;
  167. boxRect.origin.x = (self.displayBounds.size.width-width)/2.0;
  168. boxRect.size.width = width;
  169. boxRect.size.height = self.displayBounds.size.height;
  170. } else {
  171. CGFloat height = boxRect.size.height*self.displayBounds.size.width/boxRect.size.width;
  172. boxRect.origin.y = (self.displayBounds.size.height-height)/2.0;
  173. boxRect.size.height = height;
  174. boxRect.size.width = self.displayBounds.size.width;
  175. }
  176. }
  177. self.pageView.layer.transform = CATransform3DIdentity;
  178. self.pageView.frame = boxRect;
  179. self.pageView.layer.transform = CATransform3DMakeRotation(self.page.rotation*M_PI/180.0, 0, 0, 1);
  180. }];
  181. }
  182. - (void)setSelected:(BOOL)selected {
  183. [super setSelected:selected];
  184. if (selected) {
  185. self.pageView.layer.borderWidth = 1.0;
  186. self.pageView.layer.borderColor = [UIColor colorWithRed:0.0 green:124.0/255.0 blue:1.0 alpha:1.0].CGColor;
  187. self.textLabel.textColor = [UIColor colorWithRed:0.0 green:124.0/255.0 blue:1.0 alpha:1.0];
  188. } else {
  189. self.pageView.layer.borderWidth = 0.0;
  190. self.textLabel.textColor = [UIColor blackColor];
  191. }
  192. }
  193. @end