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 = 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];html

 


#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 = 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]; }
相關文章
相關標籤/搜索