12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- //
- // PDFMagnifierView.m
- // PDFReader
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // The PDF Reader Sample applications are licensed with a modified BSD license.
- // Please see License for details. This notice may not be removed from this file.
- //
- #import "PDFMagnifierView.h"
- @implementation PDFMagnifierView
- #pragma mark - Initializers
- - (instancetype)init {
- if (self = [super initWithFrame:CGRectMake(0, 0, 145, 59)]) {
- self.backgroundColor = [UIColor clearColor];
- self.scale = 1.0;
- }
- return self;
- }
- - (void)dealloc {
- _viewToMagnify = nil;
- [super dealloc];
- }
- #pragma mark - Accessors
- - (void)setStyle:(PDFMagnifierViewStyle)style {
- _style = style;
- if (PDFMagnifierViewStyleRect == style) {
- self.frame = CGRectMake(0, 0, 145, 59);
- } else if (PDFMagnifierViewStyleCircle == style) {
- self.frame = CGRectMake(0, 0, 127, 127);
- }
- }
- #pragma mark - Draw
- - (void)drawRect:(CGRect)rect {
- if (PDFMagnifierViewStyleRect == self.style) {
- CGContextRef context = UIGraphicsGetCurrentContext();
- UIImage *image = [UIImage imageNamed:@"magnifier-ranged-lo.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- [image drawInRect:self.bounds];
-
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
- CGContextRef mirrorContext = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(mirrorContext, self.bounds.size.width/2.0, self.bounds.size.height/2.0-2);
- CGContextScaleCTM(mirrorContext, self.scale, -self.scale);
- CGContextTranslateCTM(mirrorContext, -self.pointToMagnify.x, -self.pointToMagnify.y);
- [self.viewToMagnify.layer renderInContext:mirrorContext];
-
- UIImage *maskImage = [UIImage imageNamed:@"magnifier-ranged-mask.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- CGImageRef imageRef = CGBitmapContextCreateImage(mirrorContext);
- CGImageRef maskImageRef = CGImageCreateWithMask(imageRef, maskImage.CGImage);
- CGImageRelease(imageRef);
- UIGraphicsEndImageContext();
- CGContextDrawImage(context, self.bounds, maskImageRef);
- CGImageRelease(maskImageRef);
-
- image = [UIImage imageNamed:@"magnifier-ranged-hi.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- [image drawInRect:self.bounds];
- } else if (PDFMagnifierViewStyleCircle == self.style) {
- CGContextRef context = UIGraphicsGetCurrentContext();
- UIImage *image = [UIImage imageNamed:@"loupe-lo.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- [image drawInRect:self.bounds];
-
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
- CGContextRef mirrorContext = UIGraphicsGetCurrentContext();
- CGContextTranslateCTM(mirrorContext, self.bounds.size.width/2.0, self.bounds.size.height/2.0);
- CGContextScaleCTM(mirrorContext, self.scale, -self.scale);
- CGContextTranslateCTM(mirrorContext, -self.pointToMagnify.x, -self.pointToMagnify.y);
- [self.viewToMagnify.layer renderInContext:mirrorContext];
-
- UIImage *maskImage = [UIImage imageNamed:@"loupe-mask.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- CGImageRef imageRef = CGBitmapContextCreateImage(mirrorContext);
- CGImageRef maskImageRef = CGImageCreateWithMask(imageRef, maskImage.CGImage);
- CGImageRelease(imageRef);
- UIGraphicsEndImageContext();
- CGContextDrawImage(context, self.bounds, maskImageRef);
- CGImageRelease(maskImageRef);
-
- // image = [UIImage imageNamed:@"loupe-hi.png" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
- // [image drawInRect:self.bounds];
- }
- }
- @end
|