如何在WKWebView物镜c

时间:2018-08-08 10:02:36

标签: ios objective-c iphone webview wkwebview

这是我显示图像的代码。

NSString *doc = @"http://13.232.1.87:3000/images/flights/bc4952c8ec51588cdbd72d91c4e1533562273837.jpeg";

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:doc]];

[webView loadRequest:nsrequest];
[self.view addSubview:webView];

1 个答案:

答案 0 :(得分:0)

我看到您的链接指向一个简单的jpg图像。 然后,您可以使用

将html代码(而不是直接链接到该图片)输入WKWebView。
[webView  loadHTMLString:@"<html>...</html>" baseURL:nil];

[edit]或者,如果链接始终指向图像,则可以考虑甚至不使用WKWebView并将其替换为UIImage:

NSURL *url = [NSURL URLWithString:doc];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];

或者,简而言之:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:doc]]];

然后像使用WKWebView一样设置图像框架(或约束)