需求:在项目中,使用WKWebView加载html的富文本,只点击图片的时候展示图片,其他的不显示
问题:第一次点击用SDWebImage,不加载网络图片,以后再点击可以正常显示图片,SDWebImage进行了缓存,但是第一次图片还没加载的时候代码同步线程展示的
代码:

UIImageView *imaView = [[UIImageView alloc] initWithFrame:webView.bounds];

[imaView sd_setImageWithURL:[NSURL URLWithString:url.absoluteString]];

[webView addSubview:imaView];

NSMutableArray *arrM = [NSMutableArray array];
MJPhoto *p = [[MJPhoto alloc] init];
p.index = ;
p.image = imaView.image;
// p.url = [NSURL URLWithString:url.absoluteString]; // 图片路径
// [p.srcImageView sd_setImageWithURL:[NSURL URLWithString:url.absoluteString]];
// p.srcImageView = webView.superview; // 来源于哪个UIImageView
[arrM addObject:p]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
brower.photos = arrM;
brower.currentPhotoIndex = ;
[brower show]; 修改后的代码:(正常显示) [imaView sd_setImageWithURL:[NSURL URLWithString:url.absoluteString] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSMutableArray *arrM = [NSMutableArray array];
MJPhoto *p = [[MJPhoto alloc] init];
p.index = ;
p.image = imaView.image;
[arrM addObject:p]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
brower.photos = arrM;
brower.currentPhotoIndex = ;
[brower show];
}];
完整项目代码:

[self.webView loadHTMLString:[self.goodsModel.goodsDesc exceptNull] baseURL:nil];

#pragma mark - WKNavigationDelegate
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { }
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (webView.canGoBack == YES) {
NSLog(@"关闭");
} else {
NSLog(@"隐藏");
} NSURL *url = navigationAction.request.URL;
NSString *scheme = [url scheme];
if ([scheme isEqualToString:@"tel"]) {
// NSString *resourceSpecifier = [url resourceSpecifier];
// NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", resourceSpecifier];
// // dispatch_async(dispatch_get_global_queue(0, 0), ^{
// // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
// // });
decisionHandler(WKNavigationActionPolicyCancel);
// if ([[UIApplication sharedApplication ] canOpenURL:[NSURL URLWithString:callPhone]]) {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
// }else{
// [self HUDShow:@"设备不支持拨号功能" delay:2];
// }
// decisionHandler(WKNavigationActionPolicyAllow);
return;
}else if([url.absoluteString isEqualToString:@"about:blank"]) {//主页面加载内容
decisionHandler(WKNavigationActionPolicyAllow);
return;
}else if ([url.absoluteString rangeOfString:@"//itunes.apple.com/"].location != NSNotFound) {
// if ([[UIApplication sharedApplication] canOpenURL:url]) {
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"即将离开APP\n打开AppStore" message:nil preferredStyle:UIAlertControllerStyleAlert];
// [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//
// }]];
// [alertController addAction:[UIAlertAction actionWithTitle:@"允许" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// if (@available(iOS 10.0, *)) {
// [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {}];
// } else {
// // Fallback on earlier versions
// }
// }]];
// [self presentViewController:alertController animated:YES completion:nil];
// decisionHandler(WKNavigationActionPolicyCancel);
// return;
// }else{
decisionHandler(WKNavigationActionPolicyCancel);
return;
// }
}else if ([url.absoluteString hasPrefix:@"http://"] && [url.absoluteString hasPrefix:@"https://"]) {
// if ([[UIApplication sharedApplication] canOpenURL:url]) {
// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"即将离开APP\n打开其他应用" message:nil preferredStyle:UIAlertControllerStyleAlert];
// [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//
// }]];
// [alertController addAction:[UIAlertAction actionWithTitle:@"允许" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// [[UIApplication sharedApplication] openURL:url];
// }]];
// [self presentViewController:alertController animated:YES completion:nil];
// decisionHandler(WKNavigationActionPolicyCancel);
// return;
// }else{
decisionHandler(WKNavigationActionPolicyCancel);
return;
// }
}else if ([url.absoluteString rangeOfString:@"jpg"].location != NSNotFound || [url.absoluteString rangeOfString:@"png"].location != NSNotFound || [url.absoluteString rangeOfString:@"jpeg"].location != NSNotFound) {
UIImageView *imaView = [[UIImageView alloc] initWithFrame:webView.bounds];
[imaView sd_setImageWithURL:[NSURL URLWithString:url.absoluteString] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSMutableArray *arrM = [NSMutableArray array];
MJPhoto *p = [[MJPhoto alloc] init];
p.index = ;
p.image = imaView.image;
[arrM addObject:p]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
brower.photos = arrM;
brower.currentPhotoIndex = ;
[brower show];
}];
[webView addSubview:imaView]; [imaView removeFromSuperview];
}
decisionHandler(WKNavigationActionPolicyCancel);
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{
decisionHandler(WKNavigationResponsePolicyAllow);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
NSLog(@"didStartProvisionalNavigation: %@", navigation);
}
// 接收到服务器跳转请求之后调用 (服务器端redirect),不一定调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
NSLog(@"didReceiveServerRedirectForProvisionalNavigation: %@", navigation);
}
// 页面加载失败时调用(暂时)
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
NSLog(@"didFailProvisionalNavigation: %@navigation, error: %@", navigation, error);
if (error.code == NSURLErrorCancelled) {
return;
}
}
// 当内容开始返回(获取到网页内容) 时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{
NSLog(@"didCommitNavigation: %@", navigation);
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
NSLog(@"didFinish: %@; stillLoading:%@", [webView URL], (webView.loading?@"NO":@"YES")); // 禁止系统的弹框
[self webkitTouchCallout:webView];
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
NSLog(@"didFailNavigation: %@, error %@", navigation, error);
}
// 对于HTTPS的都会触发此代理,如果不要求验证,传默认就行
// 如果需要证书验证,与使用AFN进行HTTPS证书验证是一样的
//- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
//
//}
// 9.0才能使用,web内容处理中断时会触发
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0))
{
[webView reload];
}
#pragma mark - WKNavigationDelegate UI界面相关,原生控件支持,三种提示框:输入、确认、警告。首先将web提示框拦截然后再做处理。
//WKWebView does not open any links which have target="_blank" aka. 'Open in new Window' attribute in their HTML -Tag.
// 创建一个新的WebView
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[webView loadRequest:navigationAction.request];
}
return nil;
}
- (void)webViewDidClose:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0))
{ }
- (void)webkitTouchCallout:(WKWebView *)webView
{
// 不执行前段界面弹出列表的JS代码
[webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';" completionHandler:nil];
[webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none'" completionHandler:nil];
}

iOS - 使用SDWebImage缓存图片,MJPhotoBrowser展示图片的问题的更多相关文章

  1. base64编码加密图片和展示图片

    base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一.base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于 网络传输.由于这些二进制字符在传输协议中属于控制字符, ...

  2. iOS 使用SDwebImage缓存图片并在断网时候显示

     [_loadImageView setShowActivityIndicatorView:YES];    [_loadImageView setIndicatorStyle:UIActivityI ...

  3. Webi Report 展示 图片链接 (Image Link)

    最近由于项目需求,在生成的Webi Report中需要增加一列展示相关数据系统的图片链接,要求用户可以通过点击图片链接展示图片. 方法如下: 1,首先找到系统中图片,查看随机一张图片的属性,找到图片的 ...

  4. iOS利用SDWebImage图片下载缓存

    一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...

  5. SDWebImage缓存图片的机制(转)

    SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...

  6. SDWebImage缓存图片的机制

    SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...

  7. AJ学IOS(55)多线程网络之图片下载框架之SDWebImage

    AJ分享,必须精品 效果: 代码: - (NSArray *)apps { if (!_apps) { NSArray *dictArray = [NSArray arrayWithContentsO ...

  8. sdwebimage缓存图片

    当使用SDWebImage时,如果用相同图片名的图片替换掉了原始缓存的图片,当再次请求的时候,还是使用的缓存图片,图片不会发生改变 原因:图片在NSCache中是以absolute url作为key存 ...

  9. iOS UIImage UIImageView 展示图片 不变形 处理

    展示图片 时候 固定 了 imageView  的大小  图片 也裁剪了 尽量保持比例 可是 还是失真 变形了 这张图 ui 要求展示的UIimageView 大小 是固定 的  ,传过来的 图片 是 ...

随机推荐

  1. 前后端分离结构中使用shiro进行权限控制

    前阵子在前后端分离项目中集成shiro项目,折腾了一下子,参考了网上一些博客,发现大多都还是之前传统的模式,并不适用于前后端分离结构.今天抽空整理了下demo,方便以后使用以及后来人参考. 一.spr ...

  2. 云打印 对Echo的Beta产品测试报告

    云打印 对Echo的Beta产品测试报告 课程名称:软件工程1916|W(福州大学) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标:作业集合 团队队员 队员学号 队员姓名 个人博 ...

  3. git 学习笔记 —— 获取远端分支并修改后提交至远端仓库

    笔者最近进行开发过程中,所有参与者的代码需要通过 git 上传到远端仓库中,不同的模块对应不同的 git 分支,不同模块的数据需要从远端仓库中获取.这里记录下笔者从远端仓库中获取分支数据,进行修改,最 ...

  4. Sitemap Error : XML declaration allowed only at the start of the document解决方法

    今天ytkah的客户反馈说他的xml网站地图有问题,提示Sitemap Error : XML declaration allowed only at the start of the documen ...

  5. 【转】Web测试中定位bug方法

    在web测试过程中,经常会遇到页面中内容或数据显示错误,甚至不显示,第一反应就是BUG,进一步了解这个BUG的问题出在那里,是测试人员需要掌握的,可以简单的使用浏览器自带开发者工具.数据库工具配合去排 ...

  6. 如何保护你的 Python 代码 (一)—— 现有加密方案

    https://zhuanlan.zhihu.com/p/54296517 0 前言 去年11月在PyCon China 2018 杭州站分享了 Python 源码加密,讲述了如何通过修改 Pytho ...

  7. npm install 命令。默认会找到当前路径下的package.json。然后安装其中的依赖

    npm install 命令.默认会找到当前路径下的package.json.然后安装其中的依赖 By default, npm install will install all modules li ...

  8. 常见WinDbg问题及解决方案

    当你调试一个程序时,你最不想处理的是调试器不能正常工作.当你试图集中精力跟踪一个bug时,总是会因为次要的问题而被忽略,尤其是当调试器的问题导致你失去一个重新编程或者浪费了大量的时间等待调试器完成它, ...

  9. P3386 【模板】二分图匹配(匈牙利算法)

    题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正整数u,v,表示u,v有一条连边 ...

  10. jQuery实现列表框双向选择操作

    对列表框的操作经常碰到过这样的应用:从左侧的列表框中选中要选的项添加到右侧列表框中,然后提交最终选择的项,对误操作而选中的项还可以执行移除操作.在很多系统中应用比如说求职网站的选择意向工作地区,QQ好 ...