在移動互聯網時代,越來越多的業務需要在線簽名功能,其中包括合同簽署、授權認證等。而在iOS系統中,也提供了一種快速實現在線簽名的方法,下面將詳細介紹其原理和使用方式。
一、原理
iOS系統中的在線簽名功能主要基于Core Graphics框架的手寫筆畫識別技術,其原理可以簡述為以下幾個步驟:
1.獲取手寫筆畫:通過UIResponder類中的touch事件獲取用戶手寫筆畫的坐標點,并將其保存在一個數組中;
2.繪制手寫筆畫:在UIView中重寫drawRect方法,將保存的手寫筆畫坐標點連成線條,形成手寫筆畫的圖形;
3.識別手寫筆畫:通過Core Graphics框架中的UIGraphicsGetImageFromCurrentImageContext方法將手寫筆畫轉換成一張圖片,并將其傳遞給Core ML框架中的手寫筆畫識別模型進行識別;
4.返回識別結果:將識別結果返回給應用程序,實現在線簽名功能。
二、使用方式
在iOS系統中實現在線簽名功能的方式有多種,下面介紹其中一種比較簡單的實現方式。
1.創建一個UIView子類,用于繪制手寫筆畫;
2.在UIView子類中添加以下代碼,實現手寫筆畫的繪制:
“`
– (void)drawRect
:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetLineCap(context, kCGLineCapRound);
for (int i=0; i
CGPoint startPoint = [[self.points objectAtIndex:i] CGPointValue];
CGPoint endPoint = [[self.points objectAtIndex:i+1] CGPointValue];
CGContextBeginPath(context);
CGContextMoveToPoint(context, startPoint.x, startPoint.y)蘋果證書信任設置里面的;
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
}
}
“`
3.在UIViewController中添加以下代碼,實現手寫筆畫的識別:
“`
– (IBAction)recognizeSignature:(id)sender {
UIGraphicsBeginImageContext(self.signatureView.bounds.size);
[self.signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *signatureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *scaledImage = [self scaleImage:signatureImage toSize:CGSizeMake(28, 28)];
NSError *error;
MNIST *model = [[MNIST alloc] init];
MNISTOutput *output = [model predictionFromImage:scaledImage error:&error];
if (error) {
NSLog(@”Error: %@”, error.localizedDescription);
} else {
ios開發者簽名udd NSLog(@”Output: %@”, output.classLabel);
}
}
– (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
“`
其中,MNIST是一個用于手寫數字識別的Core ML模型,可以通過Core ML框架導入使用。
通過以上步驟,即可實現iOS系統中的在線簽名功能。
三、總結
iOS系統中的在線簽名功能主要基于Core Graphics框架的手寫筆畫識別技術,其原理比較簡單,使用也比較方便。通過以上介紹,相信讀者能夠了解iOS系統中在線簽名的原理和使用方式,為移動互聯網業務的開發提供更多便利。
一門APP證書制作工具(https://platform.yimenapp.com/)提供APP證書在線制作,支持蘋果證書、安卓證書在線一鍵快捷制作。工具完全免費,注冊成為一門APP開發者即可使用,全中文化云端APP證書工具。
一鍵制作IOS蘋果證書,包含appstore上架證書、開發環境測試證書、ADhoc生產環境測試證書、在線生成P12開發者證書證書,P12推送證書、P8推送證書,快捷綁定UDID、自定義包名Bundle ID、在線獲取描述文件;
一鍵制作安卓證書,支持自定義安卓包名、簽名文件密碼(storepass)、別名(alias)、別名密碼(keypass)、公司/機構名稱 (O)、部門 (OU)、國家/地區 (C)、省份 (ST)、城市 (L)、郵箱 (E)、以及安卓證書有效期。