長(zhǎng)按點(diǎn)擊網(wǎng)頁(yè)的圖片保存,二維碼識(shí)別

1.網(wǎng)頁(yè)添加長(zhǎng)按手勢(shì)

手勢(shì)觸發(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:@"識(shí)別圖中的二維碼" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"識(shí)別圖中的二維碼");

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

}];

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

{

NSLog(@"是網(wǎng)址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[alertController addAction:otherActionTwo];

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

}

else

{

NSLog(@"不是網(wǎng)址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

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

}

}

}


2.導(dǎo)入ZBar SDK

下面的方法是zbar識(shí)別圖片網(wǎng)址,如果圖片是二維碼的話,會(huì)返回一個(gè)網(wǎng)址,如果不是二維碼的話,不會(huì)返回網(wǎng)址(這個(gè)邏輯,期待被打破)

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

NSString *qrResult = nil;

//iOS8及以上可以使用系統(tǒng)自帶的識(shí)別二維碼圖片接口,但此api有問(wèn)題,在一些機(jī)型上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:@"識(shí)別圖中的二維碼" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)

{

NSLog(@"識(shí)別圖中的二維碼");

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

}];

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

{

NSLog(@"是網(wǎng)址");

[alertController addAction:cancelAction];

[alertController addAction:otherAction];

[alertController addAction:otherActionTwo];

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

}

else

{

NSLog(@"不是網(wǎng)址");

[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)自帶的識(shí)別二維碼圖片接口,但此api有問(wèn)題,在一些機(jī)型上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


相關(guān)網(wǎng)站

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

http://www.lxweimin.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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容