今天遇到一个问题,要在webview里面点击放大图片。因为是新手,我便在网上搜集了一下。其中感受这种方法仍是比较不错的。原文地址:http://my.oschina.net/linxiaoxi1993/blog/465905?p={{page}}web
亲测,没问题。ide
第一步在webView加载完成的代理方法添加这些方法this
- (void)webViewDidFinishLoad:(UIWebView *)webView{ //调整字号 NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '95%'"; [webView stringByEvaluatingJavaScriptFromString:str]; //js方法遍历图片添加点击事件 返回图片个数 static NSString * const jsGetImages = @"function getImages(){\ var objs = document.getElementsByTagName(\"img\");\ for(var i=0;i<objs.length;i++){\ objs[i].onclick=function(){\ document.location=\"myweb:imageClick:\"+this.src;\ };\ };\ return objs.length;\ };"; [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法 //注入自定义的js方法后别忘了调用 不然不会生效(不调用也同样生效了,,,不明白) NSString *resurlt = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"]; //调用js方法 NSLog(@"---调用js方法--%@ %s jsMehtods_result = %@",self.class,__func__,resurlt); }
在处理webView事件的代理方法lua
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //将url转换为string NSString *requestString = [[request URL] absoluteString]; // NSLog(@"requestString is %@",requestString); //hasPrefix 判断建立的字符串内容是否以pic:字符开始 if ([requestString hasPrefix:@"myweb:imageClick:"]) { NSString *imageUrl = [requestString substringFromIndex:@"myweb:imageClick:".length]; // NSLog(@"image url------%@", imageUrl); if (_bgView) { //设置不隐藏,还原放大缩小,显示图片 _bgView.hidden = NO; _imgView.frame = CGRectMake(10, 10, KScreenWidth-40, 220); [_imgView sd_setImageWithURL:[NSURL URLWithString:imageUrl]]; //[_imgView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:LOAD_IMAGE(@"house_moren")]; } else [self showBigImage:imageUrl];//建立视图并显示图片 return NO; } return YES; }
放大图片的一些操做url
#pragma mark 显示大图片 -(void)showBigImage:(NSString *)imageUrl{ //建立灰色透明背景,使其背后内容不可操做 _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)]; [_bgView setBackgroundColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.7]]; [self.view addSubview:_bgView]; //建立边框视图 UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-20, 240)]; //将图层的边框设置为圆脚 borderView.layer.cornerRadius = 8; borderView.layer.masksToBounds = YES; //给图层添加一个有色边框 borderView.layer.borderWidth = 8; borderView.layer.borderColor = [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.7] CGColor]; [borderView setCenter:_bgView.center]; [_bgView addSubview:borderView]; //建立关闭按钮 UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [closeBtn setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal]; closeBtn.backgroundColor = [UIColor redColor]; [closeBtn addTarget:self action:@selector(removeBigImage) forControlEvents:UIControlEventTouchUpInside]; [closeBtn setFrame:CGRectMake(borderView.frame.origin.x+borderView.frame.size.width-20, borderView.frame.origin.y-6, 26, 27)]; [_bgView addSubview:closeBtn]; //建立显示图像视图 _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, CGRectGetWidth(borderView.frame)-20, CGRectGetHeight(borderView.frame)-20)]; _imgView.userInteractionEnabled = YES; [_imgView sd_setImageWithURL:[NSURL URLWithString:imageUrl]]; //[imgView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:LOAD_IMAGE(@"house_moren")]; [borderView addSubview:_imgView]; //添加捏合手势 [_imgView addGestureRecognizer:[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)]]; } //关闭按钮 -(void)removeBigImage { _bgView.hidden = YES; } - (void) handlePinch:(UIPinchGestureRecognizer*) recognizer { //缩放:设置缩放比例 recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); recognizer.scale = 1; }