How do I get the Image of the Ink Signature from a SignatureViewController?

Option 1

Subclass the SignatureViewController and override its done(_:) method, to access its drawView in order to 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:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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 being presented in a context in which a Document is not 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 the DrawView:

Copy
1
2
3
4
5
6
7
8
9
10
11
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)
        }
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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, take a look at AccessInkSignatureBiometricDataExample.swift and PSCSignAllPagesExample.m from our PSPDFKit Catalog app.