@property (nonatomic,copy) NSString *imgURL;
UIWebView *_web;
NSInteger _gesState;
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[MBProgressHUD hideHUD];
//实现图片长按识别功能
static NSString* const kTouchJavaScriptString=
@"document.ontouchstart=function(event){\
x=event.targetTouches[0].clientX;\
y=event.targetTouches[0].clientY;\
document.location=\"myweb:touch:start:\"+x+\":\"+y;};\
document.ontouchmove=function(event){\
x=event.targetTouches[0].clientX;\
y=event.targetTouches[0].clientY;\
document.location=\"myweb:touch:move:\"+x+\":\"+y;};\
document.ontouchcancel=function(event){\
document.location=\"myweb:touch:cancel\";};\
document.ontouchend=function(event){\
document.location=\"myweb:touch:end\";};";
[webView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];//注入js方法
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// add by zcj 实现图片长按识别功能
NSString *requestString = request.URL.absoluteString;
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0]
isEqualToString:@"myweb"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
{
//NSLog(@"you are touching!");
if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"])
{
_gesState = UIGestureRecognizerStateBegan;
float ptX =[[components objectAtIndex:3]floatValue];
float ptY =[[components objectAtIndex:4]floatValue];
NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY];
NSString * tagName = [_web stringByEvaluatingJavaScriptFromString:js];
if ([tagName isEqualToString:@"IMG"]) {
self.imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY];
}
if (self.imgURL) {
[self performSelector:@selector(handleLongTouch) withObject:nil afterDelay:1.0];
}
}
else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"])
{
_gesState = UIGestureRecognizerStateChanged;
}
}
else if ([(NSString*)[components objectAtIndex:2]isEqualToString:@"end"]) {
_gesState = UIGestureRecognizerStateEnded;
}
}// ]end
return YES;
}
- (void)handleLongTouch {
if (self.imgURL && _gesState == UIGestureRecognizerStateBegan) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSString *urlToSave = [_web stringByEvaluatingJavaScriptFromString:self.imgURL];
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]];
UIImage* image = [UIImage imageWithData:data];
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
@"CIDetectorAccuracy", @"CIDetectorAccuracyHigh",nil];
CIDetector *detector = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
detector = [CIDetector detectorOfType:CIDetectorTypeQRCode
context:nil
options:options];
NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]] ;
// 识别图中二维码
UIAlertAction *judgeCode = [UIAlertAction actionWithTitle:@"识别图中二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
CIQRCodeFeature *feature = [features objectAtIndex:0];
NSString *scannedResult = feature.messageString;
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scannedResult]]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:scannedResult]];
}else{
[MBProgressHUD showError:@"无法识别的网址"];
}
}];
// 保存图片到手机
UIAlertAction *saveImage = [UIAlertAction actionWithTitle:@"保存到手机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}];
// 取消
UIAlertAction *cancell = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
if (features.count >= 1) {
[alertController addAction:judgeCode];
}
[alertController addAction:saveImage];
[alertController addAction:cancell];
[self presentViewController:alertController animated:YES completion:nil];
}
}
实现长按识别webview中的二维码
最新推荐文章于 2024-05-17 03:15:03 发布