CPDFThumbnailViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // PDFThumbnailViewController.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 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 "UIViewController+LeftItem.h"
  15. #import "CPDFColorUtils.h"
  16. #import <ComPDFKit/ComPDFKit.h>
  17. @interface CPDFThumbnailViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
  18. @property (nonatomic, strong) UICollectionView *collectionView;
  19. @end
  20. @implementation CPDFThumbnailViewController
  21. #pragma mark - Initializers
  22. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  23. if (self = [super init]) {
  24. _pdfView = pdfView;
  25. }
  26. return self;
  27. }
  28. #pragma mark - Accessors
  29. #pragma mark - UIViewController Methods
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. [self changeleftItem];
  34. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  35. layout.itemSize = CGSizeMake(110, 185);
  36. layout.sectionInset = UIEdgeInsetsMake(10, 5, 5, 5);
  37. layout.minimumInteritemSpacing = 5;
  38. layout.minimumLineSpacing = 5;
  39. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  40. self.collectionView.frame = self.view.bounds;
  41. [self.collectionView registerClass:[CPDFThumbnailViewCell class] forCellWithReuseIdentifier:@"thumnailCell"];
  42. self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  43. self.collectionView.delegate = self;
  44. self.collectionView.dataSource = self;
  45. self.collectionView.alwaysBounceVertical = YES;
  46. if (@available(iOS 11.0, *)) {
  47. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
  48. }
  49. self.view.backgroundColor = [UIColor whiteColor];
  50. self.collectionView.backgroundColor = [UIColor colorWithRed:0.804 green:0.804 blue:0.804 alpha:1];
  51. [self.view addSubview:self.collectionView];
  52. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  53. self.title = NSLocalizedString(@"Thumbnails", nil);
  54. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"CPDFEditClose" inBundle:[NSBundle bundleForClass:CPDFThumbnailViewController.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStyleDone target:self action:@selector(buttonItemClicked_back:)];
  55. self.navigationItem.leftBarButtonItem = nil;
  56. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  57. }
  58. - (void)buttonItemClicked_back:(id)button {
  59. if([self.delegate respondsToSelector:@selector(thumbnailViewControllerDismiss:)]) {
  60. [self.delegate thumbnailViewControllerDismiss:self];
  61. } else {
  62. [self dismissViewControllerAnimated:YES completion:^{
  63. }];
  64. }
  65. }
  66. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  67. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  68. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  69. }
  70. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  71. {
  72. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  73. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  74. CGFloat mWidth = fmin(width, height);
  75. CGFloat mHeight = fmax(width, height);
  76. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width,traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.9 : mHeight*0.9);
  77. }
  78. - (void)viewWillAppear:(BOOL)animated {
  79. [super viewWillAppear:animated];
  80. [self.collectionView reloadData];
  81. if(self.pdfView.document) {
  82. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
  83. [self.collectionView selectItemAtIndexPath:indexPath
  84. animated:NO
  85. scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  86. }
  87. }
  88. - (void)viewDidAppear:(BOOL)animated {
  89. [super viewDidAppear:animated];
  90. if(self.pdfView.document) {
  91. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.pdfView.currentPageIndex inSection:0];
  92. [self.collectionView scrollToItemAtIndexPath:indexPath
  93. atScrollPosition:UICollectionViewScrollPositionCenteredVertically
  94. animated:NO];
  95. }
  96. }
  97. #pragma mark - Class method
  98. - (void)setCollectViewSize:(CGSize)size {
  99. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  100. layout.itemSize = size;
  101. layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  102. layout.minimumInteritemSpacing = 5;
  103. layout.minimumLineSpacing = 5;
  104. self.collectionView.collectionViewLayout = layout;
  105. }
  106. #pragma mark - UICollectionViewDataSource
  107. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  108. return self.pdfView.document.pageCount;
  109. }
  110. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  111. CPDFThumbnailViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thumnailCell" forIndexPath:indexPath];
  112. CPDFPage *page = [self.pdfView.document pageAtIndex:indexPath.item];
  113. CGSize pageSize = [self.pdfView.document pageSizeAtIndex:indexPath.item];
  114. CGFloat multiple = MAX(pageSize.width / 110, pageSize.height / 173);
  115. cell.imageSize = CGSizeMake(pageSize.width / multiple, pageSize.height / multiple);
  116. [cell setNeedsLayout];
  117. cell.imageView.image = [page thumbnailOfSize:CGSizeMake(pageSize.width / multiple, pageSize.height / multiple)];
  118. cell.textLabel.text = [NSString stringWithFormat:@"%@",@(indexPath.item+1)];
  119. return cell;
  120. }
  121. #pragma mark - UICollectionViewDelegate
  122. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  123. if([self.delegate respondsToSelector:@selector(thumbnailViewController:pageIndex:)]) {
  124. [self.delegate thumbnailViewController:self pageIndex:indexPath.row];
  125. }
  126. }
  127. @end