WebView 点击浏览大图

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    

    

    NSString *js = @"function imgAutoFit() { \

    var imgs = document.getElementsByTagName('img'); \

    for (var i = 0; i < imgs.length; ++i) {\

    var img = imgs[i];   \

    img.style.width = %f;\

    } \

    }";

    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];

    [webView stringByEvaluatingJavaScriptFromString:js];

    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];

    

    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];

    CGFloat height = [height_str floatValue];

    NSLog(@"height-----%f",height);

    _webViewHeight.constant = height + 10;

    _heightWeb = height + 10;

    

    CGFloat  aa = _heightWeb  + 70;

    [Center postNotificationName:BDPhotoDetailWebFinidhHeight object:self userInfo:@{@"heightWeb":[NSNumber numberWithFloat:aa]}];

    

    

    

    

    //js方法遍历图片添加点击事件 返回图片个数

    static  NSString * const jsGetImages =

    @"function getImages(){\

    var objs = document.getElementsByTagName(\"img\");\

    for(var i=0;i<objs.length;i++){\

    objs[i].οnclick=function(){\

    document.location=\"myweb:imageClick:\"+this.src;\

    };\

    };\

    return objs.length;\

    };";

    

    [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    

    NSString *requestString = [[request URL] absoluteString];

    //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:DEFAULT_IMG];

        }

        else

            [self showBigImage:imageUrl];//创建视图并显示图片

        

        return NO;

    }

    return YES;

}

#pragma mark 显示大图片

-(void)showBigImage:(NSString *)imageUrl{


    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20,SCREEN_HEIGHT-100 )];

    imgView.userInteractionEnabled = YES;

    [imgView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        [UIImageView showImage:imgView];

    }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值