gpt4 book ai didi

javascript - 不允许加载本地资源 : file:///var/mobile/Containers/Data/Application/03F63826-7849-47C3-A6DF-E1EB1113FF0A/Documents/xxx. png

转载 作者:行者123 更新时间:2023-11-28 23:34:14 26 4
gpt4 key购买 nike

XCode 10.1
iOS 12.0.1

我使用wkwebview的函数“evaluateJavaScript”来回调图片url,让wkwebview可以显示这张图片。
它在模拟器中成功运行。
但它在设备上失败了。
这有什么问题吗?

模拟器 url 很不错:

file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/8FB6E110-F30F-425D-9011-A196E78BE7CB/data/Containers/Data/Application/E122A32D-7E21-4EBA-A9E7-BC15E8468A11/Documents/20190409150915.png

设备 url 失败:

Not allowed to load local resource: file:///var/mobile/Containers/Data/Application/03F63826-7849-47C3-A6DF-E1EB1113FF0A/Documents/20190408203417.png

NSArray *imagesArray = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"pickerImages"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"file://%@/",documentsDirectory];
NSString *imagesString = pickerImagesArray.firstObject;
NSString *jscript = @"";

if ([imagesString isEqualToString:@""])
{
jscript = [NSString stringWithFormat:@"getimage_cb('%@')" , @""];
} else {
NSString *output = [NSString stringWithFormat:@"%@%@",filePath,imagesString];
jscript = [NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"getimage_cb('%@')" , output]];
}

[self.myWkwebView evaluateJavaScript:jscript completionHandler:^(id object, NSError * _Nullable error) {
}];

最佳答案

关于这个特定主题,SO 上有相互矛盾的答案。

我读到的是,您可以直接从应用程序的包中加载图像,但不能从其文档目录中加载图像。

一种可能的解决方法是将图像加载到 UIImage 实例中,将其转换为 base64 并将其传递给 evaluateJavascript 方法:

    NSArray *imagesArray = [[NSArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"pickerImages"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagesString = [pickerImagesArray firstObject];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, imagesString];

//Init UIImage Instance
UIImage *uiImageInstance = [[UIImage alloc] initWithContentsOfFile:filePath];

//Get PNG Representation as NSData
NSData *data = UIImagePNGRepresentation(uiImageInstance);

//Convert to Base64
NSString *base64Str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

//Construct the attribute value we'll pass in the javascript
NSString *attribute = [NSString stringWithFormat: @"data:image/png;base64,%@", base64Str];

//Set the image within the webview
[self.myWkwebView evaluateJavascript: [NSString stringWithFormat: @"document.getElementById('img').setAttribute( 'src', %@);", attribute] completionHandler:^(id object, NSError * _Nullable error) {}];

关于javascript - 不允许加载本地资源 : file:///var/mobile/Containers/Data/Application/03F63826-7849-47C3-A6DF-E1EB1113FF0A/Documents/xxx. png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com