下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
项目里面需要实现,点击webVIEw里面的图片,看大图并且能够放大缩小效果,在此记录下实现过程。- (voID)webVIEwDIDFinishLoad:(UIWebVIEw *)aWebVIEw { //调整字号 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);}
- (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, SCREEN_WIDTH-40, 220); [imgVIEw setimageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:LOAD_IMAGE(@"house_moren")]; } else [self showBigImage:imageUrl];//创建视图并显示图片 return NO; } return YES;}
#pragma mark 显示大图片-(voID)showBigImage:(Nsstring *)imageUrl{ //创建灰色透明背景,使其背后内容不可 *** 作 bgVIEw = [[UIVIEw alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; [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, SCREEN_WIDTH-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, CGRectGetWIDth(borderVIEw.frame)-20, CGRectGetHeight(borderVIEw.frame)-20)]; imgVIEw.userInteractionEnabled = YES; [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;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS开发 之 WebView点击图片看大图效果全部内容,希望文章能够帮你解决iOS开发 之 WebView点击图片看大图效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)