PDFMagnifierView.m 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // PDFMagnifierView.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 "PDFMagnifierView.h"
  11. @implementation PDFMagnifierView
  12. #pragma mark - Initializers
  13. - (instancetype)init {
  14. if (self = [super initWithFrame:CGRectMake(0, 0, 145, 59)]) {
  15. self.backgroundColor = [UIColor clearColor];
  16. self.scale = 1.0;
  17. }
  18. return self;
  19. }
  20. - (void)dealloc {
  21. _viewToMagnify = nil;
  22. [super dealloc];
  23. }
  24. #pragma mark - Accessors
  25. - (void)setStyle:(PDFMagnifierViewStyle)style {
  26. _style = style;
  27. if (PDFMagnifierViewStyleRect == style) {
  28. self.frame = CGRectMake(0, 0, 145, 59);
  29. } else if (PDFMagnifierViewStyleCircle == style) {
  30. self.frame = CGRectMake(0, 0, 127, 127);
  31. }
  32. }
  33. #pragma mark - Draw
  34. - (void)drawRect:(CGRect)rect {
  35. if (PDFMagnifierViewStyleRect == self.style) {
  36. CGContextRef context = UIGraphicsGetCurrentContext();
  37. UIImage *image = [UIImage imageNamed:@"magnifier-ranged-lo.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  38. [image drawInRect:self.bounds];
  39. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
  40. CGContextRef mirrorContext = UIGraphicsGetCurrentContext();
  41. CGContextTranslateCTM(mirrorContext, self.bounds.size.width/2.0, self.bounds.size.height/2.0-2);
  42. CGContextScaleCTM(mirrorContext, self.scale, -self.scale);
  43. CGContextTranslateCTM(mirrorContext, -self.pointToMagnify.x, -self.pointToMagnify.y);
  44. [self.viewToMagnify.layer renderInContext:mirrorContext];
  45. UIImage *maskImage = [UIImage imageNamed:@"magnifier-ranged-mask.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  46. CGImageRef imageRef = CGBitmapContextCreateImage(mirrorContext);
  47. CGImageRef maskImageRef = CGImageCreateWithMask(imageRef, maskImage.CGImage);
  48. CGImageRelease(imageRef);
  49. UIGraphicsEndImageContext();
  50. CGContextDrawImage(context, self.bounds, maskImageRef);
  51. CGImageRelease(maskImageRef);
  52. image = [UIImage imageNamed:@"magnifier-ranged-hi.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  53. [image drawInRect:self.bounds];
  54. } else if (PDFMagnifierViewStyleCircle == self.style) {
  55. CGContextRef context = UIGraphicsGetCurrentContext();
  56. UIImage *image = [UIImage imageNamed:@"loupe-lo.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  57. [image drawInRect:self.bounds];
  58. UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
  59. CGContextRef mirrorContext = UIGraphicsGetCurrentContext();
  60. CGContextTranslateCTM(mirrorContext, self.bounds.size.width/2.0, self.bounds.size.height/2.0);
  61. CGContextScaleCTM(mirrorContext, self.scale, -self.scale);
  62. CGContextTranslateCTM(mirrorContext, -self.pointToMagnify.x, -self.pointToMagnify.y);
  63. [self.viewToMagnify.layer renderInContext:mirrorContext];
  64. UIImage *maskImage = [UIImage imageNamed:@"loupe-mask.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  65. CGImageRef imageRef = CGBitmapContextCreateImage(mirrorContext);
  66. CGImageRef maskImageRef = CGImageCreateWithMask(imageRef, maskImage.CGImage);
  67. CGImageRelease(imageRef);
  68. UIGraphicsEndImageContext();
  69. CGContextDrawImage(context, self.bounds, maskImageRef);
  70. CGImageRelease(maskImageRef);
  71. // image = [UIImage imageNamed:@"loupe-hi.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
  72. // [image drawInRect:self.bounds];
  73. }
  74. }
  75. @end