iOS - 使用SDWebImage缓存图片,MJPhotoBrowser展示图片的问题
需求:在项目中,使用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展示图片的问题的更多相关文章
- base64编码加密图片和展示图片
base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一.base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于 网络传输.由于这些二进制字符在传输协议中属于控制字符, ...
- iOS 使用SDwebImage缓存图片并在断网时候显示
[_loadImageView setShowActivityIndicatorView:YES]; [_loadImageView setIndicatorStyle:UIActivityI ...
- Webi Report 展示 图片链接 (Image Link)
最近由于项目需求,在生成的Webi Report中需要增加一列展示相关数据系统的图片链接,要求用户可以通过点击图片链接展示图片. 方法如下: 1,首先找到系统中图片,查看随机一张图片的属性,找到图片的 ...
- iOS利用SDWebImage图片下载缓存
一.我们先来了解一下SDWebImage的使用: 1.导入框架,引入头文件: #import "UIImageView+WebCache.h" 也可以直接使用CocoaPods来引 ...
- SDWebImage缓存图片的机制(转)
SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...
- SDWebImage缓存图片的机制
SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...
- AJ学IOS(55)多线程网络之图片下载框架之SDWebImage
AJ分享,必须精品 效果: 代码: - (NSArray *)apps { if (!_apps) { NSArray *dictArray = [NSArray arrayWithContentsO ...
- sdwebimage缓存图片
当使用SDWebImage时,如果用相同图片名的图片替换掉了原始缓存的图片,当再次请求的时候,还是使用的缓存图片,图片不会发生改变 原因:图片在NSCache中是以absolute url作为key存 ...
- iOS UIImage UIImageView 展示图片 不变形 处理
展示图片 时候 固定 了 imageView 的大小 图片 也裁剪了 尽量保持比例 可是 还是失真 变形了 这张图 ui 要求展示的UIimageView 大小 是固定 的 ,传过来的 图片 是 ...
随机推荐
- python中pop()与split()的用法
imglist = ['11.jpg','12.jpg','13.jpg','14.jpg','2.jpg','1.jpg',] print(str(imglist)) a = str(imglist ...
- 某个新闻网站抓去自媒体账号 queryId js破解
第一步: 发现加密 第二部:搜索加密参数 queryId 第三部:找到js核心加密代码 第四部:代码实现 window = {} function utf8ToBase64(t) { console. ...
- 《团队名称》第八次团队作业:Alpha冲刺day3
项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8-软件测试与ALPHA冲刺 团队名称 快活帮 作业学习目标 (1)掌握 ...
- egg 阻止 sql 注入,相关文章
egg 阻止 sql 注入,相关文章 网址 注意!!我们极其不建议开发者拼接 sql 语句,这样很容易引起 sql 注入!!如果必须要自己拼接 sql 语句,请使用 mysql.escape 方法. ...
- java 泛型 类型作为参量 Class<T> transform
Class<T> transform T:作为类型,用于定义变量: transform:作为具体类的类:用于创建实例. 类型信息是脱敏的具体类: 可以使用class的具体功能: 不能使用具 ...
- SG函数的理解集应用
转载自知乎牛客竞赛——博弈论入门(函数讲解+真题模板) SG函数 作用 对于一个状态i为先手必胜态当且仅当SG(i)!=0. 转移 那怎么得到SG函数尼. SG(i)=mex(SG(j))(状态i可以 ...
- hdu4027-Can you answer these queries? -(线段树+剪枝)
题意:给n个数,m个操作,分两种操作,一种是将一段区间的每个数都开根号,另一种是查询区间和. 解题:显然对每个数开根号不能用lazy的区间更新.一个一个更新必然爆时间,对1开根号还是1,如果一段区间都 ...
- [Algorithm] 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 从Hello World 来讲解线程
从一个经典的例子开始:一个打印“Hello World.”的程序.一个非常简单的在单线程中运行的Hello World程序如下所示,当我们谈到多线程时,它可以作为一个基准. #include<i ...
- 修改 oracle xe 默认中文字符集成为:SIMPLIFIED CHINESE_CHINA.ZHS16GBK
修改 oracle xe 默认中文字符集成为:SIMPLIFIED CHINESE_CHINA.ZHS16GBK Oracle XE 执行安装程序后,很简单的默认为 SIMPLIFIED CHINE ...