长按点击网页的图片保存,二维码识别

1.网页添加长按手势

手势触发事件方法代码如下


-(void)handleSingleTap:(UITapGestureRecognizer *)sender

{

CGPoint pt = [sender locationInView:_webView];

NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];

NSString *urlToSave = [_webView stringByEvaluatingJavaScriptFromString:imgURL];

NSLog(@"image url=%@", urlToSave);

if (urlToSave.length > 0)

{

//        NSString *turnString=[self decodeQRImageWith:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]]]];

//        NSString *turnString=@"https://itunes.apple.com/cn/app/zhuo-yue-hui/id1159685485?mt=8";

NSString *turnString=@"http://mp.weixin.qq.com/s?__biz=MzA5ODU1MzQxMg==&mid=2650930327&idx=3&sn=7c91c7e233d623dad1b48e80f837c9e6&chksm=8b7a87bcbc0d0eaaf5f7d507aa00f906e477551c67805f3a85bd2a4949223dfdf52320864b47&scene=0#wechat_redirect";

NSLog(@"theturnimg---%@",turnString);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)

{

NSLog(@"取消");

}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"保存图片");

UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]]];

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}];

UIAlertAction *otherActionTwo = [UIAlertAction actionWithTitle:@"识别图中的二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"识别图中的二维码");

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:turnString]];

}];

if ([turnString hasPrefix:@"http"])

{

NSLog(@"是网址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[alertController addAction:otherActionTwo];

[self presentViewController:alertController animated:YES completion:nil];

}

else

{

NSLog(@"不是网址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[self presentViewController:alertController animated:YES completion:nil];

}

}

}


2.导入ZBar SDK

下面的方法是zbar识别图片网址,如果图片是二维码的话,会返回一个网址,如果不是二维码的话,不会返回网址(这个逻辑,期待被打破)

- (NSString *)decodeQRImageWith:(UIImage*)aImage {

NSString *qrResult = nil;

//iOS8及以上可以使用系统自带的识别二维码图片接口,但此api有问题,在一些机型上detector为nil。

//    if (iOS8_OR_LATER) {

//        CIContext *context = [CIContext contextWithOptions:nil];

//        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

//        CIImage *image = [CIImage imageWithCGImage:aImage.CGImage];

//        NSArray *features = [detector featuresInImage:image];

//        CIQRCodeFeature *feature = [features firstObject];

//

//        qrResult = feature.messageString;

//    } else {

ZBarReaderController* read = [ZBarReaderController new];

CGImageRef cgImageRef = aImage.CGImage;

ZBarSymbol* symbol = nil;

for(symbol in [read scanImage:cgImageRef]) break;

qrResult = symbol.data ;

return qrResult;

}

3.保存图片到本地

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

{

if (error)

{

NSLog(@"Error");

[SVProgressHUD showErrorWithStatus:@"保存失败"];

}

else

{

NSLog(@"OK");

[SVProgressHUD showSuccessWithStatus:@"成功保存"];

}

}


附上代码

////  ChamberIntroduceViewController.m//  BnPlus////  Created by 密码123 on 16/11/4.//  Copyright © 2016年 Sgs. All rights reserved.//#import "ZBarSDK.h"#import "ChamberIntroduceViewController.h"@interface ChamberIntroduceViewController (){

UIWebView *_webView;

}

@end

@implementation ChamberIntroduceViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self initWeb];

}

-(void)initWeb

{

_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,NaviBarHeight+20, kScreenWidth, kScreenHeight-NaviBarHeight-20)];

_webView.backgroundColor = [UIColor whiteColor];

_webView.delegate=self;

[self.view addSubview:_webView];

NSString *urlString=[NSString stringWithFormat:@"%@/web/allyexplain?allyid=%@",weixinURL,[UserInfoModel sharedUserInfoModel].currentChamberModel.allyId];

//    NSString *urlString=@"http://wxpay.weixin.qq.com/pub_v2/pay/wap.v2.php";

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

[self addTapOnWebView];

}

-(void)addTapOnWebView

{

UILongPressGestureRecognizer *longPressGR =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];

longPressGR.minimumPressDuration=0.48;

longPressGR.delegate=self;

[_webView addGestureRecognizer:longPressGR];

longPressGR.cancelsTouchesInView = NO;

}

#pragma mark- TapGestureRecognizer

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender

{

CGPoint pt = [sender locationInView:_webView];

NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];

NSString *urlToSave = [_webView stringByEvaluatingJavaScriptFromString:imgURL];

NSLog(@"image url=%@", urlToSave);

if (urlToSave.length > 0)

{

NSString *turnString=[self decodeQRImageWith:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]]]];

//        NSString *turnString=@"https://itunes.apple.com/cn/app/zhuo-yue-hui/id1159685485?mt=8";

NSLog(@"theturnimg---%@",turnString);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)

{

NSLog(@"取消");

}];

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"保存图片");

UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]]];

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}];

UIAlertAction *otherActionTwo = [UIAlertAction actionWithTitle:@"识别图中的二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"识别图中的二维码");

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:turnString]];

}];

if ([turnString hasPrefix:@"http"])

{

NSLog(@"是网址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[alertController addAction:otherActionTwo];

[self presentViewController:alertController animated:YES completion:nil];

}

else

{

NSLog(@"不是网址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[self presentViewController:alertController animated:YES completion:nil];

}

}

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

{

if (error)

{

NSLog(@"Error");

[SVProgressHUD showErrorWithStatus:@"保存失败"];

}

else

{

NSLog(@"OK");

[SVProgressHUD showSuccessWithStatus:@"成功保存"];

}

}

#pragma mark - webdelegate

-(void) webViewDidFinishLoad:(UIWebView *)webView

{

[UIApplication sharedApplication].networkActivityIndicatorVisible =NO;

NSString *title2 = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

[self setNavtitleItem:title2];

}

#pragma mark -ZBardelegate

- (NSString *)decodeQRImageWith:(UIImage*)aImage {

NSString *qrResult = nil;

//iOS8及以上可以使用系统自带的识别二维码图片接口,但此api有问题,在一些机型上detector为nil。

//    if (iOS8_OR_LATER) {

//        CIContext *context = [CIContext contextWithOptions:nil];

//        CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

//        CIImage *image = [CIImage imageWithCGImage:aImage.CGImage];

//        NSArray *features = [detector featuresInImage:image];

//        CIQRCodeFeature *feature = [features firstObject];

//

//        qrResult = feature.messageString;

//    } else {

ZBarReaderController* read = [ZBarReaderController new];

CGImageRef cgImageRef = aImage.CGImage;

ZBarSymbol* symbol = nil;

for(symbol in [read scanImage:cgImageRef]) break;

qrResult = symbol.data ;

return qrResult;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

demohttps://github.com/Yijijianghu/CheckQrCodeImageOfWebPage


相关网站

http://www.jianshu.com/p/39dd77f359bd

http://www.jianshu.com/p/48e44fe67c1d

http://blog.csdn.net/jamy08/article/details/50424407

http://download.csdn.net/download/qq_28643245/8759313

http://blog.csdn.net/loving_ios/article/details/49872529

http://www.mincoder.com/article/4419.shtml

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容