CPDFStampViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //
  2. // CPDFStampViewController.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 "CPDFStampViewController.h"
  13. #import "CStampFileManger.h"
  14. #import "CStampCollectionViewCell.h"
  15. #import "CPDFColorUtils.h"
  16. #import "CStampButton.h"
  17. #import "CStampTextViewController.h"
  18. #import "CStampTextViewController.h"
  19. #import "CCustomizeStampTableViewCell.h"
  20. #import "CStampPreview.h"
  21. #import <ComPDFKit/ComPDFKit.h>
  22. #import <compdfkit_tools/AAPLCustomPresentationController.h>
  23. #define kStamp_Cell_Height 60
  24. PDFAnnotationStampKey const PDFAnnotationStampKeyType = @"PDFAnnotationStampKeyType";
  25. PDFAnnotationStampKey const PDFAnnotationStampKeyImagePath = @"PDFAnnotationStampKeyImagePath";
  26. PDFAnnotationStampKey const PDFAnnotationStampKeyText = @"PDFAnnotationStampKeyText";
  27. PDFAnnotationStampKey const PDFAnnotationStampKeyShowDate = @"PDFAnnotationStampKeyShowDate";
  28. PDFAnnotationStampKey const PDFAnnotationStampKeyShowTime = @"PDFAnnotationStampKeyShowTime";
  29. PDFAnnotationStampKey const PDFAnnotationStampKeyStyle = @"PDFAnnotationStampKeyStyle";
  30. PDFAnnotationStampKey const PDFAnnotationStampKeyShape = @"PDFAnnotationStampKeyShape";
  31. @interface CPDFStampViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIPopoverPresentationControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITableViewDelegate, UITableViewDataSource, CStampTextViewControllerDelegate, CCustomizeStampTableViewCellDelegate>
  32. @property (nonatomic, strong) UICollectionView *collectView;
  33. @property (nonatomic, strong) UITableView *tableView;
  34. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  35. @property (nonatomic, strong) NSArray *standardArray;
  36. @property (nonatomic, strong) NSArray *customTextArray;
  37. @property (nonatomic, strong) NSArray *customImageArray;
  38. @property (nonatomic, strong) NSMutableDictionary *imgDicCache;
  39. @property (nonatomic, strong) UIButton *backBtn;
  40. @property (nonatomic, strong) UILabel *titleLabel;
  41. @property (nonatomic, strong) UIButton *createButton;
  42. @property (nonatomic, strong) UIView *standardView;
  43. @property (nonatomic, strong) UIView *customizeView;
  44. @property (nonatomic, strong) CStampFileManger *stampFileManager;
  45. @property (nonatomic, strong) CStampButton *textButton;
  46. @property (nonatomic, strong) CStampButton *imageButton;
  47. @property (nonatomic, strong) UIView *modelView;
  48. @end
  49. @implementation CPDFStampViewController
  50. #pragma mark - ViewController Methods
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. // Do any additional setup after loading the view.
  54. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  55. self.titleLabel = [[UILabel alloc] init];
  56. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  57. self.titleLabel.text = NSLocalizedString(@"Stamp", nil);
  58. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  59. self.titleLabel.font = [UIFont systemFontOfSize:20];
  60. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  61. [self.view addSubview:self.titleLabel];
  62. self.backBtn = [[UIButton alloc] init];
  63. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  64. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  65. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  66. [self.view addSubview:self.backBtn];
  67. NSArray *segmmentArray = [NSArray arrayWithObjects:NSLocalizedString(@"Standard", nil), NSLocalizedString(@"Customize", nil), nil];
  68. self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmmentArray];
  69. self.segmentedControl.selectedSegmentIndex = 0;
  70. self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  71. [self.segmentedControl addTarget:self action:@selector(segmentedControlValueChanged_singature:) forControlEvents:UIControlEventValueChanged];
  72. [self.view addSubview:self.segmentedControl];
  73. // StandardView
  74. [self createStandardView];
  75. // CustomizeView
  76. [self createCustomizeView];
  77. // Data
  78. NSMutableArray *array = [NSMutableArray array];
  79. for (int i=1; i<13; i++) {
  80. NSString *tPicName = nil;
  81. if (i<10) {
  82. tPicName = [NSString stringWithFormat:@"CPDFStampImage-0%d.png",i];
  83. } else {
  84. tPicName = [NSString stringWithFormat:@"CPDFStampImage-%d.png",i];
  85. }
  86. [array addObject:tPicName];
  87. }
  88. [array addObjectsFromArray:@[@"CPDFStampImage-13", @"CPDFStampImage-14", @"CPDFStampImage-15", @"CPDFStampImage-16", @"CPDFStampImage-18", @"CPDFStampImage-20", @"CPDFStampImage_chick", @"CPDFStampImage_cross", @"CPDFStampImage_circle"]];
  89. self.standardArray = array;
  90. self.imgDicCache = [NSMutableDictionary dictionary];
  91. self.stampFileManager = [[CStampFileManger alloc] init];
  92. [self.stampFileManager readStampDataFromFile];
  93. self.customTextArray = [self.stampFileManager getTextStampData];
  94. self.customImageArray = [self.stampFileManager getImageStampData];
  95. [self createGestureRecognizer];
  96. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  97. }
  98. - (void)viewWillLayoutSubviews {
  99. [super viewWillLayoutSubviews];
  100. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  101. self.segmentedControl.frame = CGRectMake(50, 50, self.view.frame.size.width-100, 30);
  102. if (@available(iOS 11.0, *)) {
  103. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  104. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 200 - self.view.safeAreaInsets.bottom, 50, 50);
  105. self.textButton.frame = CGRectMake(self.view.frame.size.width - 180 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 320 - self.view.safeAreaInsets.bottom, 160, 40);
  106. self.imageButton.frame = CGRectMake(self.view.frame.size.width - 180 - self.view.safeAreaInsets.right, self.view.bounds.size.height - 270 - self.view.safeAreaInsets.bottom, 160, 40);
  107. } else {
  108. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 100, 5, 50, 50);
  109. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 100, 50, 50);
  110. self.textButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 220, 160, 40);
  111. self.imageButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 170, 160, 40);
  112. }
  113. self.modelView.frame = CGRectMake(0, -200, self.view.bounds.size.width, self.view.bounds.size.height+200);
  114. }
  115. - (void)viewWillAppear:(BOOL)animated {
  116. [super viewWillAppear:animated];
  117. [self.collectView reloadData];
  118. }
  119. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  120. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  121. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  122. }
  123. #pragma mark - Private Methods
  124. - (void)createStandardView {
  125. self.standardView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100)];
  126. self.standardView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  127. [self.view addSubview:self.standardView];
  128. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  129. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  130. layout.minimumInteritemSpacing = 10;
  131. layout.minimumLineSpacing = 10;
  132. layout.itemSize = CGSizeMake(170,80);
  133. layout.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
  134. self.collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  135. self.collectView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  136. self.collectView.delegate = self;
  137. self.collectView.dataSource = self;
  138. self.collectView.backgroundColor = [UIColor clearColor];
  139. [self.collectView registerClass:[StampCollectionHeaderView class]
  140. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  141. withReuseIdentifier:@"header"];
  142. [self.collectView registerClass:[StampCollectionHeaderView1 class]
  143. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  144. withReuseIdentifier:@"header1"];
  145. [self.collectView registerClass:[CStampCollectionViewCell class]
  146. forCellWithReuseIdentifier:@"TStampViewCell"];
  147. if (@available(iOS 11.0, *)) {
  148. _collectView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
  149. }
  150. [self.standardView addSubview:self.collectView];
  151. }
  152. - (void)createCustomizeView {
  153. self.customizeView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height)];
  154. self.customizeView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  155. [self.view addSubview:self.customizeView];
  156. self.customizeView.hidden = YES;
  157. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  158. self.tableView.delegate = self;
  159. self.tableView.dataSource = self;
  160. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  161. self.tableView.rowHeight = 100;
  162. [self.customizeView addSubview:self.tableView];
  163. self.modelView = [[UIView alloc] init];
  164. self.modelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  165. [self.customizeView addSubview:self.modelView];
  166. self.modelView.hidden = YES;
  167. self.createButton = [[UIButton alloc] init];
  168. self.createButton.layer.cornerRadius = 25.0;
  169. self.createButton.clipsToBounds = YES;
  170. [self.createButton setImage:[UIImage imageNamed:@"CPDFSignatureImageAdd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  171. self.createButton.backgroundColor = [UIColor blueColor];
  172. [self.createButton addTarget:self action:@selector(buttonItemClicked_create:) forControlEvents:UIControlEventTouchUpInside];
  173. [self.customizeView addSubview:self.createButton];
  174. self.textButton = [[CStampButton alloc] init];
  175. [self.textButton.stampBtn setImage:[UIImage imageNamed:@"CPDFStampImageText" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  176. self.textButton.titleLabel.text = NSLocalizedString(@"Text Stamp", nil);
  177. [self.textButton.stampBtn addTarget:self action:@selector(buttonItemClicked_text:) forControlEvents:UIControlEventTouchUpInside];
  178. [self.customizeView addSubview:self.textButton];
  179. self.textButton.hidden = YES;
  180. self.imageButton = [[CStampButton alloc] init];
  181. [self.imageButton.stampBtn setImage:[UIImage imageNamed:@"CPDFStampImageImage" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  182. self.imageButton.titleLabel.text = NSLocalizedString(@"Image Stamp", nil);
  183. [self.imageButton.stampBtn addTarget:self action:@selector(buttonItemClicked_image:) forControlEvents:UIControlEventTouchUpInside];
  184. [self.customizeView addSubview:self.imageButton];
  185. self.imageButton.hidden = YES;
  186. }
  187. - (void)createGestureRecognizer {
  188. [self.createButton setUserInteractionEnabled:YES];
  189. [self.modelView setUserInteractionEnabled:YES];
  190. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panaddBookmarkBtn:)];
  191. [self.createButton addGestureRecognizer:panRecognizer];
  192. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapModelView:)];
  193. [self.modelView addGestureRecognizer:tapRecognizer];
  194. }
  195. - (void)panaddBookmarkBtn:(UIPanGestureRecognizer *)gestureRecognizer {
  196. CGPoint point = [gestureRecognizer translationInView:self.view];
  197. CGFloat newX = self.createButton.center.x + point.x;
  198. CGFloat newY = self.createButton.center.y + point.y;
  199. if (CGRectContainsPoint(self.view.frame, CGPointMake(newX, newY))) {
  200. self.createButton.center = CGPointMake(newX, newY);
  201. }
  202. [gestureRecognizer setTranslation:CGPointZero inView:self.view];
  203. }
  204. - (void)tapModelView:(UIPanGestureRecognizer *)gestureRecognizer {
  205. self.textButton.hidden = YES;
  206. self.modelView.hidden = YES;
  207. self.imageButton.hidden = YES;
  208. }
  209. - (void)createImageSignature {
  210. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  211. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  212. imagePickerController.delegate = self;
  213. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  214. [self presentViewController:imagePickerController animated:YES completion:nil];
  215. }];
  216. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  217. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  218. imagePickerController.delegate = self;
  219. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  220. imagePickerController.allowsEditing = YES;
  221. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  222. [self presentViewController:imagePickerController animated:YES completion:nil];
  223. }];
  224. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  225. style:UIAlertActionStyleCancel
  226. handler:nil];
  227. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
  228. message:nil
  229. preferredStyle:UIAlertControllerStyleActionSheet];
  230. [actionSheet addAction:cameraAction];
  231. [actionSheet addAction:photoAction];
  232. [actionSheet addAction:cancelAction];
  233. actionSheet.modalPresentationStyle = UIModalPresentationPopover;
  234. [self presentViewController:actionSheet animated:YES completion:nil];
  235. }
  236. - (UIImage *)compressImage:(UIImage *)image {
  237. CGFloat maxWH = kStamp_Cell_Height;
  238. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
  239. maxWH *=[UIScreen mainScreen].scale;
  240. CGFloat imageScale = 1.0;
  241. if (image.size.width > maxWH || image.size.height>maxWH)
  242. imageScale = MIN(maxWH / image.size.width, maxWH / image.size.height);
  243. CGSize newSize = CGSizeMake(image.size.width * imageScale, image.size.height * imageScale);
  244. UIGraphicsBeginImageContext(newSize);
  245. [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  246. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  247. UIGraphicsEndImageContext();
  248. return newImage;
  249. }
  250. #pragma mark - UIImagePickerControllerDelegate
  251. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  252. [picker dismissViewControllerAnimated:YES completion:nil];
  253. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  254. UIImageOrientation imageOrientation = image.imageOrientation;
  255. if (imageOrientation!=UIImageOrientationUp) {
  256. UIGraphicsBeginImageContext(image.size);
  257. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  258. image = UIGraphicsGetImageFromCurrentImageContext();
  259. UIGraphicsEndImageContext();
  260. }
  261. NSData *imageData = UIImagePNGRepresentation(image);
  262. if (imageData == nil || [imageData length] <= 0) {
  263. return;
  264. }
  265. image = [UIImage imageWithData:imageData];
  266. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  267. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  268. if (imageRef) {
  269. image = [UIImage imageWithCGImage:imageRef];
  270. CGImageRelease(imageRef);
  271. }
  272. NSString *tPath = [self.stampFileManager saveStampWithImage:image];
  273. if (tPath) {
  274. NSMutableDictionary *tStampItem = [[NSMutableDictionary alloc] init];
  275. [tStampItem setObject:tPath forKey:@"path"];
  276. [self.stampFileManager insertStampItem:tStampItem type:PDFStampCustomType_Image];
  277. [self.tableView reloadData];
  278. }
  279. }
  280. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  281. [picker dismissViewControllerAnimated:YES completion:nil];
  282. }
  283. #pragma mark - Protect Methods
  284. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  285. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 370 : 850);
  286. }
  287. #pragma mark - Action
  288. - (void)buttonItemClicked_back:(id)sender {
  289. [self dismissViewControllerAnimated:YES completion:nil];
  290. }
  291. - (void)buttonItemClicked_create:(id)sender {
  292. self.textButton.hidden = !self.textButton.hidden;
  293. self.modelView.hidden = !self.modelView.hidden;
  294. self.imageButton.hidden = !self.imageButton.hidden;
  295. }
  296. - (void)segmentedControlValueChanged_singature:(id)sender {
  297. if (self.segmentedControl.selectedSegmentIndex == 0) {
  298. self.standardView.hidden = NO;
  299. self.customizeView.hidden = YES;
  300. } else {
  301. self.standardView.hidden = YES;
  302. self.customizeView.hidden = NO;
  303. }
  304. }
  305. - (void)buttonItemClicked_text:(id)sender {
  306. self.textButton.hidden = YES;
  307. self.modelView.hidden = YES;
  308. self.imageButton.hidden = YES;
  309. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  310. CStampTextViewController *stampTextVC = [[CStampTextViewController alloc] init];
  311. stampTextVC.delegate = self;
  312. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:stampTextVC presentingViewController:self];
  313. stampTextVC.transitioningDelegate = presentationController;
  314. [self presentViewController:stampTextVC animated:YES completion:nil];
  315. }
  316. - (void)buttonItemClicked_image:(id)sender {
  317. self.textButton.hidden = YES;
  318. self.modelView.hidden = YES;
  319. self.imageButton.hidden = YES;
  320. [self createImageSignature];
  321. }
  322. #pragma mark - UICollectionViewDataSource
  323. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  324. return 1;
  325. }
  326. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  327. return self.standardArray.count;
  328. }
  329. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  330. CStampCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TStampViewCell" forIndexPath:indexPath];
  331. cell.editing = NO;
  332. cell.stampImage.image = [UIImage imageNamed:self.standardArray[indexPath.item] inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
  333. return cell;
  334. }
  335. #pragma mark - UICollectionViewDelegate
  336. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  337. [self dismissViewControllerAnimated:YES completion:^{
  338. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  339. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:[NSDictionary dictionary]];
  340. }
  341. }];
  342. }
  343. #pragma mark - UITableViewDataSource
  344. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  345. return 2;
  346. }
  347. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  348. switch (section) {
  349. case 0:
  350. {
  351. return self.customTextArray.count;
  352. }
  353. break;
  354. case 1:
  355. {
  356. return self.customImageArray.count;
  357. }
  358. break;
  359. default:
  360. return 0;
  361. }
  362. }
  363. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  364. if (section == 0) {
  365. return NSLocalizedString(@"Text Stamp", nil);
  366. } else if (section == 1) {
  367. return NSLocalizedString(@"Image Stamp", nil);
  368. } else {
  369. return @"";
  370. }
  371. }
  372. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  373. CCustomizeStampTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  374. if (cell == nil) {
  375. cell = [[CCustomizeStampTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  376. }
  377. if (self.customTextArray.count>0 || self.customImageArray.count>0) {
  378. if (0 == indexPath.section) {
  379. NSDictionary *tDic = _customTextArray[indexPath.item];
  380. NSString *tText = [tDic objectForKey:@"text"];
  381. NSInteger tStyle = [[tDic objectForKey:@"style"] integerValue];
  382. NSInteger tColorStyle = [[tDic objectForKey:@"colorStyle"] integerValue];
  383. BOOL tHaveDate = [[tDic objectForKey:@"haveDate"] boolValue];
  384. BOOL tHaveTime = [[tDic objectForKey:@"haveTime"] boolValue];
  385. CStampPreview *tPreview = [[CStampPreview alloc] initWithFrame:CGRectMake(0, 0, 320, kStamp_Cell_Height)];
  386. [tPreview setTextStampText:tText];
  387. [tPreview setTextStampColorStyle:tColorStyle];
  388. [tPreview setTextStampStyle:tStyle];
  389. [tPreview setTextStampHaveDate:tHaveDate];
  390. [tPreview setTextStampHaveTime:tHaveTime];
  391. tPreview.leftMargin = 0;
  392. UIImage *tImg = [tPreview renderImage];
  393. cell.customizeStampImageView.image = tImg;
  394. } else {
  395. NSDictionary *tDic = self.customImageArray[indexPath.item];
  396. UIImage *img = [self.imgDicCache objectForKey:tDic];
  397. if (!img) {
  398. NSString *tPath = [tDic objectForKey:@"path"];
  399. NSString *tFileName = [[NSFileManager defaultManager] displayNameAtPath:tPath];
  400. NSString *tRealPath = [NSString stringWithFormat:@"%@/%@",kPDFStampDataFolder,tFileName];
  401. UIImage *tImg = [UIImage imageWithContentsOfFile:tRealPath];
  402. img = [self compressImage:tImg];
  403. [self.imgDicCache setObject:img forKey:tDic];
  404. }
  405. cell.customizeStampImageView.image = img;
  406. }
  407. }
  408. cell.deleteDelegate = self;
  409. return cell;
  410. }
  411. #pragma mark - UITableViewDelegate
  412. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  413. if (indexPath.section == 0) {
  414. NSDictionary *tDic = _customTextArray[indexPath.item];
  415. NSString *tText = [tDic objectForKey:@"text"];
  416. NSInteger tStyle = [[tDic objectForKey:@"style"] integerValue];
  417. NSInteger tColorStyle = [[tDic objectForKey:@"colorStyle"] integerValue];
  418. BOOL tHaveDate = [[tDic objectForKey:@"haveDate"] boolValue];
  419. BOOL tHaveTime = [[tDic objectForKey:@"haveTime"] boolValue];
  420. NSInteger stampStype = 0;
  421. NSInteger stampShape = 0;
  422. switch (tColorStyle) {
  423. case TextStampColorTypeBlack:
  424. {
  425. stampShape = 0;
  426. stampStype = 0;
  427. }
  428. break;
  429. case TextStampColorTypeRed:
  430. {
  431. stampStype = 1;
  432. if (TextStampTypeRight== tStyle) {
  433. stampShape = 2;
  434. } else if (TextStampTypeLeft == tStyle) {
  435. stampShape = 1;
  436. } else {
  437. stampShape = 0;
  438. }
  439. }
  440. break;
  441. case TextStampColorTypeGreen:
  442. {
  443. stampStype = 2;
  444. if (TextStampTypeRight== tStyle) {
  445. stampShape = 2;
  446. } else if (TextStampTypeLeft == tStyle) {
  447. stampShape = 1;
  448. } else {
  449. stampShape = 0;
  450. }
  451. }
  452. break;
  453. case TextStampColorTypeBlue:
  454. {
  455. stampStype = 3;
  456. if (TextStampTypeRight== tStyle) {
  457. stampShape = 2;
  458. } else if (TextStampTypeLeft == tStyle) {
  459. stampShape = 1;
  460. } else {
  461. stampShape = 0;
  462. }
  463. }
  464. break;
  465. }
  466. [self dismissViewControllerAnimated:YES completion:^{
  467. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  468. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:@{PDFAnnotationStampKeyText : tText,
  469. PDFAnnotationStampKeyShowDate : @(tHaveDate),
  470. PDFAnnotationStampKeyShowTime : @(tHaveTime),
  471. PDFAnnotationStampKeyStyle : @(stampStype),
  472. PDFAnnotationStampKeyShape : @(stampShape)}];
  473. }
  474. }];
  475. } else if (indexPath.section == 1) {
  476. NSDictionary *tDict = self.customImageArray[indexPath.row];
  477. NSString *tPath = [tDict objectForKey:@"path"];
  478. NSString *tFileName = [[NSFileManager defaultManager] displayNameAtPath:tPath];
  479. NSString *tRealPath = [NSString stringWithFormat:@"%@/%@",kPDFStampDataFolder,tFileName];
  480. [self dismissViewControllerAnimated:YES completion:^{
  481. if (self.delegate && [self.delegate respondsToSelector:@selector(stampViewController:selectedIndex:stamp:)]) {
  482. [self.delegate stampViewController:self selectedIndex:indexPath.row stamp:@{PDFAnnotationStampKeyImagePath : tRealPath}];
  483. }
  484. }];
  485. }
  486. }
  487. #pragma mark - CCustomizeStampTableViewCellDelegate
  488. - (void)customizeStampTableViewCell:(CCustomizeStampTableViewCell *)customizeStampTableViewCell {
  489. NSIndexPath *select = [self.tableView indexPathForCell:customizeStampTableViewCell];
  490. if (select.section == 0) {
  491. [self.stampFileManager removeStampItem:select.row type:PDFStampCustomType_Text];
  492. } else if (select.section == 1) {
  493. [self.stampFileManager removeStampItem:select.row type:PDFStampCustomType_Image];
  494. }
  495. [self.tableView reloadData];
  496. }
  497. #pragma mark - CStampTextViewControllerDelegate
  498. - (void)stampTextViewController:(CStampTextViewController *)stampTextViewController dictionary:(NSDictionary *)dictionary {
  499. [self.stampFileManager insertStampItem:dictionary type:PDFStampCustomType_Text];
  500. [self.tableView reloadData];
  501. }
  502. @end