zoukankan      html  css  js  c++  java
  • iOS

    需求:在项目中,使用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 = 0;
    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 = 0;
    [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 = 0; p.image = imaView.image; [arrM addObject:p]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; brower.photos = arrM; brower.currentPhotoIndex = 0; [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 打开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 打开其他应用" 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 = 0; p.image = imaView.image; [arrM addObject:p]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; brower.photos = arrM; brower.currentPhotoIndex = 0; [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]; }
  • 相关阅读:
    I.MX6 Surfaceflinger 机制
    理解 Android Fragment
    RPi 2B DDNS 动态域名
    RPi 2B IPC webcam server
    理解 Android MVP 开发模式
    I.MX6 system.img unpack repack
    can't set android permissions
    VMware Ubuntu 共享文件夹
    解决oracle数据库连接不上的问题
    perfect-scrollbar示例
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/10950966.html
Copyright © 2011-2022 走看看