// // PDFThumbnailViewController.m // compdfkit-tools // // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved. // // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. // This notice may not be removed from this file. // #import "CPDFThumbnailViewController.h" #import "CPDFThumbnailViewCell.h" #import @interface CPDFThumbnailViewController () @property (nonatomic, assign) CGPDFDocumentRef documentRef; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation CPDFThumbnailViewController #pragma mark - Initializers - (instancetype)initWithPDFView:(CPDFView *)pdfView { if (self = [super init]) { _pdfView = pdfView; } return self; } #pragma mark - Accessors - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(110, 185); layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); layout.minimumInteritemSpacing = 5; layout.minimumLineSpacing = 5; _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; [_collectionView registerClass:[CPDFThumbnailViewCell class] forCellWithReuseIdentifier:@"cell"]; _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.alwaysBounceVertical = YES; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways; } } return _collectionView; } #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:self.collectionView]; NSURL *documentURL = [self.pdfView.document documentURL]; if (documentURL) { _documentRef = CGPDFDocumentCreateWithURL((CFURLRef)documentURL); if ([self.pdfView.document password]) { CGPDFDocumentUnlockWithPassword(_documentRef, [[self.pdfView.document password] UTF8String]); } } else { _documentRef = NULL; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.collectionView reloadData]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0]; [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES]; } #pragma mark - Class method - (void)setCollectViewSize:(CGSize)size { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = size; layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5); layout.minimumInteritemSpacing = 5; layout.minimumLineSpacing = 5; self.collectionView.collectionViewLayout = layout; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return CGPDFDocumentGetNumberOfPages(_documentRef); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CPDFThumbnailViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; CPDFPage *page = [self.pdfView.document pageAtIndex:indexPath.item]; CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item]; CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173); cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple); [cell setNeedsLayout]; cell.imageView.image = [page thumbnailOfSize:CGSizeMake(pageSize.width / multiple, pageSize.height / multiple)]; cell.textLabel.text = [NSString stringWithFormat:@"%@",@(indexPath.item+1)]; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self dismissViewControllerAnimated:YES completion:^{ [self.delegate thumbnailViewController:self forPageIndex:indexPath.item]; [self.navigationController popViewControllerAnimated:YES]; }]; } @end