How Do I Get the Image of an Ink Signature from a SignatureViewController?
There are two options for getting the image of an ink signature from a SignatureViewController
.
Option 1
Subclass the SignatureViewController
and override its done(_:)
method to access its drawView
. This will create the ink signature.
Once you have the ink signature, you can use Annotation.image(size:options:)
to create a UIImage
of the ink annotation, like so:
class CustomSignatureViewController: SignatureViewController { override func done(_ sender: Any?) { super.done(sender) // The current document. let document = ... let pageInfo = document.pageInfoForPage(at:0)! let pointSequences = self.drawView.pointSequences.map{ $0.map({ NSValue.valueWithDrawingPoint($0) }) } let pdfSpaceLines = ConvertViewLines(pdfLines: pointSequences, pageInfo: pageInfo, viewBounds: self.drawView.frame) let lines = pdfSpaceLines.map { $0.map({ $0.drawingPointValue }) } let inkAnnotation = InkAnnotation(lines: lines) // Create the `UIImage` from the ink annotation in your `SignatureViewController`. let image = inkAnnotation.image(size: inkAnnotation.boundingBox.size, options: nil) } }
@interface CustomSignatureViewController : PSPDFSignatureViewController @end @implementation CustomSignatureViewController - (void)done:(id)sender { [super done:sender]; // The current document. PSPDFDocument *document = ... PSPDFPageInfo *pageInfo = [document pageInfoForPageAtIndex:0]; NSArray<NSArray<NSValue *> *> *pointSequences = self.drawView.pointSequences; NSArray<NSArray<NSValue *> *> *lines = PSPDFConvertViewLinesToPDFLines(pointSequences, pageInfo, (CGRect){CGPointZero, pageInfo.size}); PSPDFInkAnnotation *inkAnnotation = [[PSPDFInkAnnotation alloc] initWithLines:lines]; // Create the `UIImage` from the ink annotation in your `PSPDFSignatureViewController`. UIImage *image = [inkAnnotation imageWithSize:inkAnnotation.boundingBox.size options:nil]; } @end
Option 2
If, for whatever reason, the SignatureViewController
is presented in a context in which a Document
isn’t available to get a reference PDFPageInfo
object to perform the annotation calculations, you can call DrawView.drawHierarchy(in:afterScreenUpdates:)
within an image rendering context to get a direct image out of DrawView
:
class CustomSignatureViewController: SignatureViewController { override func done(_ sender: Any?) { super.done(sender) let rect = ... let renderer = UIGraphicsImageRenderer(size: rect.size) let image = renderer.image { _ in self.drawView.drawHierarchy(in: rect, afterScreenUpdates: true) } } }
@interface CustomSignatureViewController : PSPDFSignatureViewController @end @implementation CustomSignatureViewController - (void)done:(id)sender { [super done:sender]; CGRect rect = ... UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:rect.size]; UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { [self.drawView drawViewHierarchyInRect:rect afterScreenUpdates:YES]; }]; } @end
For more details about accessing properties from a SignatureViewController
and its drawView
property, take a look at SignAllPagesExample.swift
from our PSPDFKit Catalog app.