WKWebView 不允许直接从文件存储(文档目录)访问文件,要从文档目录访问文件,您必须使用loadFileURL:allowingReadAccessToURL:
方法,下面是我的代码。
在 ViewDidLoad
//Get Document directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
//Get html from bundle and save it to document directory
NSURL *url = [[NSBundle mainBundle] URLForResource: @"fileupload" withExtension: @"html"];
NSData *data = [[NSData alloc] initWithContentsOfFile:[url path]];
[data writeToFile:[documentsPath stringByAppendingPathComponent:@"fileupload.html"] atomically:YES];
//Get image and save it to document directory
UIImage *image = [UIImage imageNamed:@"yoga.png"];
NSData *pngData = UIImagePNGRepresentation(image);
filePath = [documentsPath stringByAppendingPathComponent:@"yoga.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
filePath = [self documentsPathForFileName:@"yoga.png"];
NSLog(@"File path: %@", [self documentsPathForFileName:@"yoga.png"]);
NSURL *fileurl = [NSURL fileURLWithPath:[documentsPath stringByAppendingPathComponent:@"fileupload.html"]];
[webView loadFileURL:fileurl allowingReadAccessToURL:[NSURL fileURLWithPath:documentsPath]];
上面的代码将图像从包保存到文档目录,还保存了一个 html 文件(将使用 img 标签加载图像)。
在didFinishNavigation下面:
//Sending Path to javascript
[webView evaluateJavaScript:[NSString stringWithFormat:@"upload('%@')", filePath] completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
NSLog(@"Java script called");
}];
获取文件 url 的实用方法
- (NSString *)documentsPathForFileName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[documentsPath stringByAppendingPathComponent:name]];
return [url absoluteString];
}
最后是具有从 webview 获取路径和加载图像的方法的 HTML 文件
<html>
<head><title>demo</title>
<script>
function upload(path)
{
alert(path);
/*
var URI = "data:image/png;base64,"+path;
convertURIToImageData(URI).then(function(imageData) {
// Here you can use imageData
console.log(imageData);
});
*/
var img = document.createElement("IMG");
img.src = path;//URI;
img.border = 1;
img.height = 750;
img.width = 900;
document.getElementById('imageDiv').appendChild(img);
}
function convertURIToImageData(URI) {
return new Promise(function(resolve, reject) {
if (URI == null) return reject();
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
image = new Image();
image.addEventListener('load', function() {
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
resolve(context.getImageData(0, 0, canvas.width, canvas.height));
}, false);
image.src = URI;
});
}
</script>
</head>
<body>
<p><h1>This is a Demo !!!</h1></p>
<div id="imageDiv"></div>
</body>
</html>
以上方法是我发现从文档目录加载图像/文件的唯一方法。
您还可以以数据 url 的形式发送图像/文件并将其传递给 javascript 方法(已经用 html 文件编写),只需将图像转换为 NSData 并将基本编码转换为字符串,javascript 会将数据 url 转换为图像对象本身.
UIImage 到数据 URL 的转换:
UIImage *img = [UIImage imageNamed:@"yoga.png"];
NSData *data = UIImageJPEGRepresentation(img, 0.90);
data = [data base64EncodedDataWithOptions:0];
NSString *dataurl = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
干杯!!!