PDFSignatureEditViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // PDFSignatureEditViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFSignatureEditViewController.h"
  11. #import "PDFSignatureDrawView.h"
  12. #import "PDFSignatureColorViewController.h"
  13. @implementation PDFSignatureNavigationController
  14. - (BOOL)shouldAutorotate {
  15. return YES;
  16. }
  17. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  18. return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
  19. }
  20. @end
  21. @interface PDFSignatureEditViewController () <UIPopoverPresentationControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  22. @property (nonatomic,assign) IBOutlet PDFSignatureDrawView *drawView;
  23. @end
  24. @implementation PDFSignatureEditViewController
  25. #pragma mark - Init Methods
  26. - (void)dealloc {
  27. Block_release(_callback);
  28. [super dealloc];
  29. }
  30. #pragma mark - UIViewController Methods
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view from its nib.
  34. self.title = NSLocalizedString(@"Signature", nil);
  35. self.navigationController.navigationBar.translucent = NO;
  36. UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  37. target:self
  38. action:@selector(buttonItemClicked_Cancel:)];
  39. self.navigationItem.leftBarButtonItem = cancelItem;
  40. [cancelItem release];
  41. UIBarButtonItem *actionItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Color", nil)
  42. style:UIBarButtonItemStylePlain
  43. target:self
  44. action:@selector(buttonItemClicked_Action:)];
  45. UIBarButtonItem *imageItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Image", nil)
  46. style:UIBarButtonItemStylePlain
  47. target:self
  48. action:@selector(buttonItemClicked_Image:)];
  49. UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
  50. target:self
  51. action:@selector(buttonItemClicked_Save:)];
  52. self.navigationItem.rightBarButtonItems = @[saveItem, imageItem, actionItem];
  53. [actionItem release];
  54. [imageItem release];
  55. [saveItem release];
  56. }
  57. #pragma mark - Button Actions
  58. - (void)buttonItemClicked_Cancel:(id)sender {
  59. [self dismissViewControllerAnimated:YES completion:^{
  60. if (self.callback) {
  61. self.callback(nil);
  62. self.callback = nil;
  63. }
  64. }];
  65. }
  66. - (IBAction)buttonItemClicked_Clear:(id)sender {
  67. [self.drawView clear];
  68. }
  69. - (void)buttonItemClicked_Save:(id)sender {
  70. UIImage *image = [self.drawView signatureImage];
  71. [self dismissViewControllerAnimated:YES completion:^{
  72. if (self.callback) {
  73. self.callback(image);
  74. self.callback = nil;
  75. }
  76. }];
  77. }
  78. - (void)buttonItemClicked_Action:(UIBarButtonItem *)barButtonItem {
  79. __block PDFSignatureColorViewController *vc = [[PDFSignatureColorViewController alloc] init];
  80. vc.color = self.drawView.color;
  81. vc.lineWidth = self.drawView.lineWidth;
  82. vc.callback = ^{
  83. self.drawView.color = vc.color;
  84. self.drawView.lineWidth = vc.lineWidth;
  85. };
  86. [vc showViewController:self inBarButtonItem:barButtonItem];
  87. [vc release];
  88. }
  89. - (void)buttonItemClicked_Image:(UIBarButtonItem *)barButtonItem {
  90. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  91. UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
  92. imagePickerController.delegate = self;
  93. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  94. [self presentViewController:imagePickerController animated:YES completion:nil];
  95. }];
  96. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  97. UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
  98. imagePickerController.delegate = self;
  99. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  100. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  101. UIPopoverPresentationController *popVC = imagePickerController.popoverPresentationController;
  102. popVC.delegate = self;
  103. popVC.barButtonItem = barButtonItem;
  104. [self presentViewController:imagePickerController animated:YES completion:nil];
  105. }];
  106. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  107. style:UIAlertActionStyleCancel
  108. handler:nil];
  109. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
  110. message:nil
  111. preferredStyle:UIAlertControllerStyleActionSheet];
  112. [actionSheet addAction:cameraAction];
  113. [actionSheet addAction:photoAction];
  114. [actionSheet addAction:cancelAction];
  115. actionSheet.modalPresentationStyle = UIModalPresentationPopover;
  116. UIPopoverPresentationController *popVC = actionSheet.popoverPresentationController;
  117. popVC.delegate = self;
  118. popVC.barButtonItem = barButtonItem;
  119. [self presentViewController:actionSheet animated:YES completion:nil];
  120. }
  121. #pragma mark - UIImagePickerControllerDelegate
  122. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  123. [picker dismissViewControllerAnimated:YES completion:nil];
  124. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  125. UIImageOrientation imageOrientation = image.imageOrientation;
  126. if (imageOrientation!=UIImageOrientationUp) {
  127. UIGraphicsBeginImageContext(image.size);
  128. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  129. image = UIGraphicsGetImageFromCurrentImageContext();
  130. UIGraphicsEndImageContext();
  131. }
  132. NSData *imageData = UIImagePNGRepresentation(image);
  133. if (imageData == nil || [imageData length] <= 0) {
  134. return;
  135. }
  136. image = [UIImage imageWithData:imageData];
  137. const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
  138. CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
  139. if (imageRef) {
  140. image = [UIImage imageWithCGImage:imageRef];
  141. CGImageRelease(imageRef);
  142. }
  143. self.drawView.image = image;
  144. }
  145. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  146. [picker dismissViewControllerAnimated:YES completion:nil];
  147. }
  148. #pragma mark - UIPopoverPresentationControllerDelegate
  149. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  150. return UIModalPresentationNone;
  151. }
  152. @end