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];
}