// // PDFPageEditCell.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 "PDFPageEditCell.h" @implementation PDFEditPage - (id)initWithPageRef:(CGPDFPageRef)pageRef { if (self = [super init]) { if (pageRef) { _pageRef = CGPDFPageRetain(pageRef); _size = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox).size; } else { _size = CGSizeMake(595, 842); } } return self; } - (id)init { if (self = [super init]) { _size = CGSizeMake(595, 842); } return self; } - (void)dealloc { if (_pageRef) { CGPDFPageRelease(_pageRef); } [_imagePath release]; [super dealloc]; } - (CGSize)sizeThatFits:(CGSize)size { CGPDFPageRef pageRef = self.pageRef; CGRect boxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox); CGAffineTransform transform = CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, CGRectMake(0, 0, size.width, size.height), 0, true); CGRect rect = CGRectApplyAffineTransform(boxRect, transform); return rect.size; } @end @interface PDFPageView : UIView @property (nonatomic,assign) CGPDFPageRef pageRef; @end @implementation PDFPageView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; tiledLayer.levelsOfDetail = 4; tiledLayer.levelsOfDetailBias = 4; tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0); } return self; } - (void)dealloc { CATiledLayer *tiledLayer = (CATiledLayer *)[self layer]; tiledLayer.contents = nil; tiledLayer.delegate = nil; [tiledLayer removeFromSuperlayer]; CGPDFPageRelease(_pageRef); [super dealloc]; } + (Class)layerClass { return [CATiledLayer class]; } + (CFTimeInterval)fadeDuration { return 0.0; } - (void)setPageRef:(CGPDFPageRef)pageRef { if (_pageRef != pageRef) { CGPDFPageRelease(_pageRef); _pageRef = CGPDFPageRetain(pageRef); } } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); CGContextFillRect(context, CGContextGetClipBoundingBox(context)); CGContextSaveGState(context); if (!self.pageRef) { return; } CGAffineTransform transform = CGPDFPageGetDrawingTransform(self.pageRef, kCGPDFCropBox, self.bounds, 0, true); CGContextSaveGState(context); CGContextTranslateCTM(context, 0, rect.size.height); CGContextScaleCTM(context, 1, -1); CGContextConcatCTM(context, transform); CGContextSetInterpolationQuality(context, kCGInterpolationLow); CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetShouldAntialias(context, NO); CGContextSetShouldSmoothFonts(context, NO); CGContextDrawPDFPage(context, self.pageRef); CGContextRestoreGState(context); } @end @interface PDFPageEditCell () @property (nonatomic,retain) UIImageView *pageView; @property (nonatomic,assign) CGRect displayBounds; @end @implementation PDFPageEditCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _displayBounds = CGRectMake(0, 0, frame.size.width, MIN(frame.size.width, frame.size.height)); _pageView = [[UIImageView alloc] initWithFrame:self.bounds]; _pageView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:_pageView]; _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_displayBounds), frame.size.width, frame.size.height-CGRectGetMaxY(_displayBounds))]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.font = [UIFont systemFontOfSize:12.0]; [self.contentView addSubview:_textLabel]; } return self; } - (void)dealloc { [_page release]; [_pageView release]; [_textLabel release]; [super dealloc]; } - (void)setPage:(PDFEditPage *)page { NSTimeInterval duration = 0.3; if (_page != page) { [_page release]; _page = [page retain]; for (UIView *view in self.pageView.subviews) { [view removeFromSuperview]; } if (self.page.pageRef) { PDFPageView *pdfPageView = [[PDFPageView alloc] initWithFrame:self.pageView.bounds]; pdfPageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; pdfPageView.pageRef = page.pageRef; [self.pageView addSubview:pdfPageView]; [pdfPageView release]; } duration = 0.0; } self.pageView.image = nil; [UIView animateWithDuration:duration animations:^{ CGRect boxRect; if (self.page.pageRef) { boxRect = CGPDFPageGetBoxRect(self.page.pageRef, kCGPDFCropBox); CGAffineTransform transform = CGPDFPageGetDrawingTransform(self.page.pageRef, kCGPDFCropBox, self.displayBounds, 0, true); boxRect = CGRectApplyAffineTransform(boxRect, transform); } else { if (self.page.imagePath) { self.pageView.image = [UIImage imageWithContentsOfFile:self.page.imagePath]; } boxRect = CGRectMake(0, 0, self.page.size.width, self.page.size.height); if (boxRect.size.width < boxRect.size.height) { CGFloat width = boxRect.size.width*self.displayBounds.size.height/boxRect.size.height; boxRect.origin.x = (self.displayBounds.size.width-width)/2.0; boxRect.size.width = width; boxRect.size.height = self.displayBounds.size.height; } else { CGFloat height = boxRect.size.height*self.displayBounds.size.width/boxRect.size.width; boxRect.origin.y = (self.displayBounds.size.height-height)/2.0; boxRect.size.height = height; boxRect.size.width = self.displayBounds.size.width; } } self.pageView.layer.transform = CATransform3DIdentity; self.pageView.frame = boxRect; self.pageView.layer.transform = CATransform3DMakeRotation(self.page.rotation*M_PI/180.0, 0, 0, 1); }]; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; if (selected) { self.pageView.layer.borderWidth = 1.0; self.pageView.layer.borderColor = [UIColor colorWithRed:0.0 green:124.0/255.0 blue:1.0 alpha:1.0].CGColor; self.textLabel.textColor = [UIColor colorWithRed:0.0 green:124.0/255.0 blue:1.0 alpha:1.0]; } else { self.pageView.layer.borderWidth = 0.0; self.textLabel.textColor = [UIColor blackColor]; } } @end