美文网首页复制粘贴iOS开发技术分享iOS_swift
长按点击网页的图片保存,二维码识别

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

作者: 奇怪的知识增加了 | 来源:发表于2016-11-15 18:45 被阅读1151次

    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

    相关文章

      网友评论

      本文标题:长按点击网页的图片保存,二维码识别

      本文链接:https://www.haomeiwen.com/subject/vcrlpttx.html