Converting UIImage to CGImageRef to get full signature image to fill signature field

Good day

I’m trying to get a signature from the signatureView to fill the whole signatureField I’ve created not matter how small the signature is, i want it to fill the whole signature field.

I tried to take a picture/ image of the signatureView and tried to crop it from where the signature starts, ends and the width and height of the signature and convert the image to CGImageRef.

The CGImageRef changes the image size to bigger size and cuts the signature to quarter of the top left corner of the signature, is there any better way to accomplish the issue here. Here is my code below:

  • (UIImage*) signatureImage {

CGRect rect = CGRectMake(m_digSigView.frame.origin.x, m_digSigView.frame.origin.y, m_digSigView.frame.size.width, m_digSigView.frame.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();

[[m_digSigView layer] renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

float minX = m_digSigView.frame.size.width;
float minY = m_digSigView.frame.size.height;
float maxX = 0;
float maxY = 0;

for (int z = 0; z < [m_digSigView.m_dig_sig_points count]; z++) {
NSValue *pointContainer = [m_digSigView.m_dig_sig_points objectAtIndex:z];

CGPoint point;
[pointContainer getValue:&point];

if (minX > point.x) {
minX = point.x;
}

if (minY > point.y) {
minY = point.y;
}

if (maxX < point.x) {
maxX = point.x;
}

if (maxY < point.y) {
maxY = point.y;
}
}

CGRect cropRect = CGRectMake(minX, minY, maxX - minX, maxY - minY);
CGImageRef imageRef = CGImageCreateWithImageInRect([capturedImage CGImage], cropRect);

CGImageRef img = capturedImage.CGImage;
size_t h = CGImageGetHeight(img);
size_t w = CGImageGetWidth(img);

UIImage* image = [UIImage imageWithCGImage:imageRef];

NSData* data = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths firstObject];

NSString *path = [NSString stringWithFormat:@"%@/MyImage.png", documentDirectory];
[data writeToFile:path atomically:YES];

return image;
}

-(void)saveAppearanceWithUIImage:(UIImage*)uiImage
{
@try
{
[m_pdfViewCtrl DocLock:YES];

PTSDFDoc* doc = [[m_pdfViewCtrl GetDoc] GetSDFDoc];

NSData* data = [self getNSDataFromUIImage:uiImage];

PTObj* o = [[PTObj alloc] init];
PTImage* trnImage = [PTImage CreateWithData:doc image_data:data image_data_size:data.length width:uiImage.size.width height:uiImage.size.height bpc:8 color_space:[PTColorSpace CreateDeviceRGB] encoder_hints:o];

[self saveAppearanceWithTrnImage:trnImage];

}
@catch (NSException *exception)
{
NSLog(@“Exception: %@: %@”,[exception name], [exception reason]);
}
@finally
{
[m_pdfViewCtrl DocUnlock];
}
[self annotationModified:m_moving_annotation onPageNumber:m_annot_page_number];
}

Hi, if your objective is to stretch the signature so that it takes up the entire signature fields space, then I believe changing the following only will do it for you. The PDF renderer will stretch the image for you.

In -(void)saveAppearanceWithTrnImage:(PTImage*)trnImage use the following code at the end.

`
[apWriter2 WritePlacedElement:apElement];
apObj = [apWriter2 End];
[apObj PutRect:@“BBox” x1:0 y1:0 x2:[[m_moving_annotation GetRect] Width] y2:[[m_moving_annotation GetRect] Height]]; // Change!
[apObj PutName:@“Subtype” name:@“Form”];
[apObj PutName:@“Type” name:@“XObject”];

[m_moving_annotation SetAppearance:apObj annot_state:e_ptnormal app_state:0];
[m_moving_annotation RefreshAppearance];
`

If the above doesn’t help, then please clarify what result you want, and then we can assist further. What is the final appearance that you are trying to achieve? How does that differ from the default implementation?

Hi Ryan

The answer you privded stretches the image to fill the signature field.

What I want to accomplish it to stretch the the signature itself (signature points) to fill the signature field, doesn’t matter how small the signature is, I want to stretch the points to fill the whole signature field .

Either use a method: -(void)saveAppearanceWithPath:(NSMutableArray*)points fromCanvasSize:(CGSize)canvasSize;

or -(void)saveAppearanceWithUIImage:(UIImage*)image; which this one I’m struggling to get signature itself centered on the image .

Please see attachment of the current results. Thank you.

Hi, the images didn’t go through. Can you try attaching them again to a post, or email them to support at pdftron.

I want to stretch the points to fill the whole signature field .

Do you really want to stretch to fill the whole field, or do you want to preserve the aspect ratio?

Also, are you dealing with both points, and images, or just points?