- (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];
}];
}