iOS实现WebView上图片放大 用法及原理

来源:互联网 发布:起名字软件 编辑:程序博客网 时间:2024/06/09 20:42

1.主要使用到的是SDWebImage 框架,所以要导入SDWebImage。且#import"UIButton+WebCache.h"

2.在自己写的webview的webViewDidFinishLoad代理方法中添加代码  原理(在加载完后给html里的img添加js点击方法,在其跳转链接里添加标示符image-preview及图片的url)

[self.webViewstringByEvaluatingJavaScriptFromString:@"function assignImageClickAction(){var imgs=document.getElementsByTagName_r('img');var length=imgs.length;for(var i=0;i];[self.webView stringByEvaluatingJavaScriptFromString:@"assignImageClickAction();"];

3.自己写一个全屏的button  imagePreviewButton ​   将button的alpha = 0.0f; 这个是展示大图消失的方法

button​点击方法中      [UIViewanimateWithDuration:0.2fanimations:^{

        self.imagePreviewButton.alpha = 0.0f;

    }];

4.在自己写的webview的shouldStartLoadWithRequest代理方法中     原理(在请求的时候抓到请求的url取得url,展示到self.imagePreviewButton ---实现方式多样)

if ([request.URL.schemeisEqualToString:@"image-preview"]) {

        NSString* path = [request.URL.absoluteStringsubstringFromIndex:[@"image-preview:"length]];

        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [self.imagePreviewButton sd_setImageWithURL:[NSURLURLWithString:path]forState:UIControlStateNormalplaceholderImage:[UIImageimageNamed:@"default"]];

        [UIViewanimateWithDuration:0.2fanimations:^{

          self.imagePreviewButton.alpha = 1.0f;

        }];

        returnNO;    }

    return YES;

写完就可以自己测试看看​   可以填http://www.chunbo.com/Act/799?pg=index&md=banner2&pt=pt2-bimg<=1 这里有图片可以供测试。

原创demo下载地址:​ https://github.com/smallmuou/PPHTMLImagePreviewDemo

0 0
原创粉丝点击