1.網頁添加長按手勢
手勢觸發(fā)事件方法代碼如下
-(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及以上可以使用系統(tǒng)自帶的識別二維碼圖片接口仰担,但此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及以上可以使用系統(tǒng)自帶的識別二維碼圖片接口摔蓝,但此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
demo:https://github.com/Yijijianghu/CheckQrCodeImageOfWebPage
相關網站
http://www.reibang.com/p/39dd77f359bd
http://www.reibang.com/p/48e44fe67c1d
http://blog.csdn.net/jamy08/article/details/50424407
http://download.csdn.net/download/qq_28643245/8759313