CPDFThumbnailViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // PDFThumbnailViewController.m
  3. // compdfkit-tools
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFThumbnailViewController.h"
  13. #import "CPDFThumbnailViewCell.h"
  14. #import <ComPDFKit/ComPDFKit.h>
  15. @interface CPDFThumbnailViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
  16. @property (nonatomic, assign) CGPDFDocumentRef documentRef;
  17. @property (nonatomic, strong) UICollectionView *collectionView;
  18. @end
  19. @implementation CPDFThumbnailViewController
  20. #pragma mark - Initializers
  21. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  22. if (self = [super init]) {
  23. _pdfView = pdfView;
  24. }
  25. return self;
  26. }
  27. #pragma mark - Accessors
  28. - (UICollectionView *)collectionView {
  29. if (!_collectionView) {
  30. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  31. layout.itemSize = CGSizeMake(110, 185);
  32. layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  33. layout.minimumInteritemSpacing = 5;
  34. layout.minimumLineSpacing = 5;
  35. _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  36. [_collectionView registerClass:[CPDFThumbnailViewCell class] forCellWithReuseIdentifier:@"cell"];
  37. _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  38. _collectionView.delegate = self;
  39. _collectionView.dataSource = self;
  40. _collectionView.alwaysBounceVertical = YES;
  41. if (@available(iOS 11.0, *)) {
  42. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
  43. }
  44. }
  45. return _collectionView;
  46. }
  47. #pragma mark - UIViewController Methods
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. // Do any additional setup after loading the view.
  51. [self.view addSubview:self.collectionView];
  52. NSURL *documentURL = [self.pdfView.document documentURL];
  53. if (documentURL) {
  54. _documentRef = CGPDFDocumentCreateWithURL((CFURLRef)documentURL);
  55. if ([self.pdfView.document password]) {
  56. CGPDFDocumentUnlockWithPassword(_documentRef, [[self.pdfView.document password] UTF8String]);
  57. }
  58. } else {
  59. _documentRef = NULL;
  60. }
  61. }
  62. - (void)viewWillAppear:(BOOL)animated {
  63. [super viewWillAppear:animated];
  64. [self.collectionView reloadData];
  65. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
  66. [self.collectionView selectItemAtIndexPath:indexPath
  67. animated:NO
  68. scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  69. }
  70. - (void)viewDidAppear:(BOOL)animated {
  71. [super viewDidAppear:animated];
  72. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
  73. [self.collectionView scrollToItemAtIndexPath:indexPath
  74. atScrollPosition:UICollectionViewScrollPositionCenteredVertically
  75. animated:YES];
  76. }
  77. #pragma mark - Class method
  78. - (void)setCollectViewSize:(CGSize)size {
  79. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  80. layout.itemSize = size;
  81. layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  82. layout.minimumInteritemSpacing = 5;
  83. layout.minimumLineSpacing = 5;
  84. self.collectionView.collectionViewLayout = layout;
  85. }
  86. #pragma mark - UICollectionViewDataSource
  87. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  88. return CGPDFDocumentGetNumberOfPages(_documentRef);
  89. }
  90. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  91. CPDFThumbnailViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  92. CPDFPage *page = [self.pdfView.document pageAtIndex:indexPath.item];
  93. CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
  94. CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
  95. cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
  96. [cell setNeedsLayout];
  97. cell.imageView.image = [page thumbnailOfSize:CGSizeMake(pageSize.width / multiple, pageSize.height / multiple)];
  98. cell.textLabel.text = [NSString stringWithFormat:@"%@",@(indexPath.item+1)];
  99. return cell;
  100. }
  101. #pragma mark - UICollectionViewDelegate
  102. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  103. [self dismissViewControllerAnimated:YES completion:^{
  104. [self.delegate thumbnailViewController:self forPageIndex:indexPath.item];
  105. [self.navigationController popViewControllerAnimated:YES];
  106. }];
  107. }
  108. @end