// // PDFSignatureEditViewController.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFSignatureEditViewController.h" #import "PDFSignatureDrawView.h" #import "PDFSignatureColorViewController.h" @implementation PDFSignatureNavigationController - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight; } @end @interface PDFSignatureEditViewController () @property (nonatomic,assign) IBOutlet PDFSignatureDrawView *drawView; @end @implementation PDFSignatureEditViewController #pragma mark - Init Methods - (void)dealloc { Block_release(_callback); [super dealloc]; } #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.title = NSLocalizedString(@"Signature", nil); self.navigationController.navigationBar.translucent = NO; UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(buttonItemClicked_Cancel:)]; self.navigationItem.leftBarButtonItem = cancelItem; [cancelItem release]; UIBarButtonItem *actionItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Color", nil) style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_Action:)]; UIBarButtonItem *imageItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Image", nil) style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_Image:)]; UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(buttonItemClicked_Save:)]; self.navigationItem.rightBarButtonItems = @[saveItem, imageItem, actionItem]; [actionItem release]; [imageItem release]; [saveItem release]; } #pragma mark - Button Actions - (void)buttonItemClicked_Cancel:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ if (self.callback) { self.callback(nil); self.callback = nil; } }]; } - (IBAction)buttonItemClicked_Clear:(id)sender { [self.drawView clear]; } - (void)buttonItemClicked_Save:(id)sender { UIImage *image = [self.drawView signatureImage]; [self dismissViewControllerAnimated:YES completion:^{ if (self.callback) { self.callback(image); self.callback = nil; } }]; } - (void)buttonItemClicked_Action:(UIBarButtonItem *)barButtonItem { __block PDFSignatureColorViewController *vc = [[PDFSignatureColorViewController alloc] init]; vc.color = self.drawView.color; vc.lineWidth = self.drawView.lineWidth; vc.callback = ^{ self.drawView.color = vc.color; self.drawView.lineWidth = vc.lineWidth; }; [vc showViewController:self inBarButtonItem:barButtonItem]; [vc release]; } - (void)buttonItemClicked_Image:(UIBarButtonItem *)barButtonItem { UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease]; imagePickerController.delegate = self; imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:imagePickerController animated:YES completion:nil]; }]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease]; imagePickerController.delegate = self; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePickerController.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popVC = imagePickerController.popoverPresentationController; popVC.delegate = self; popVC.barButtonItem = barButtonItem; [self presentViewController:imagePickerController animated:YES completion:nil]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil]; UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [actionSheet addAction:cameraAction]; [actionSheet addAction:photoAction]; [actionSheet addAction:cancelAction]; actionSheet.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popVC = actionSheet.popoverPresentationController; popVC.delegate = self; popVC.barButtonItem = barButtonItem; [self presentViewController:actionSheet animated:YES completion:nil]; } #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:nil]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; UIImageOrientation imageOrientation = image.imageOrientation; if (imageOrientation!=UIImageOrientationUp) { UIGraphicsBeginImageContext(image.size); [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } NSData *imageData = UIImagePNGRepresentation(image); if (imageData == nil || [imageData length] <= 0) { return; } image = [UIImage imageWithData:imageData]; const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255}; CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking); if (imageRef) { image = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); } self.drawView.image = image; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - UIPopoverPresentationControllerDelegate - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } @end