[英]Image access permission issue using OpenLayers
I am having a problem loading an image using the OpenLayers library.我在使用 OpenLayers 库加载图像时遇到问题。 I am trying to load an image that is stored on the device dynamically.
我正在尝试加载动态存储在设备上的图像。 If I declare the options manually everything works as expected.
如果我手动声明选项,一切都会按预期工作。 If I try to load the image through path using
file://...
I get the following error:如果我尝试使用
file://...
通过路径加载图像file://...
我收到以下错误:
Not allowed to load local resource: file:///storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png
不允许加载本地资源:file:///storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png
To resolve this error I used the Ionic Web View path converter: window.Ionic.WebView.convertFileSrc()
.为了解决这个错误,我使用了Ionic Web 视图路径转换器:
window.Ionic.WebView.convertFileSrc()
。
But this gives me another error, now related to CORS, considering that the access method now uses HTTP GET, which I quite don't understand since I trying to load a local image and not a remote one:但这给了我另一个错误,现在与 CORS 相关,考虑到访问方法现在使用 HTTP GET,我很不明白,因为我试图加载本地图像而不是远程图像:
Access to image at ' http://localhost/_app_file_/storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png ' from origin ' http://localhost:8100 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
从源' http://localhost访问' http://localhost/_app_file_/storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png '上的图像: 8100 ' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。 GET http://localhost/_app_file_/storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png net::ERR_FAILED
获取http://localhost/_app_file_/storage/emulated/0/Android/data/io.ionic.starter/files/projects/1/layers/volume.png net::ERR_FAILED
If I include the file in the assets folder and try to upload it, everything works as expected, but it's not how I want it to work.如果我将文件包含在资产文件夹中并尝试上传它,一切都会按预期工作,但这不是我想要的工作方式。
Working code (manually configured):工作代码(手动配置):
let layerImage = new ImageLayer({
source: new Static({
url: 'assets/layers/volume.png',
crossOrigin: '',
projection: 'EPSG:31982',
imageExtent: layerExtent
}),
name: "layerImage",
visible: true,
});
this.map.addLayer(layerImage);
Not working code (dinamically configured inside a for loop):不工作的代码(在 for 循环中动态配置):
let filePath = this.win.Ionic.WebView.convertFileSrc(this.file.externalDataDirectory + 'projects/' + this.projectId + '/layers/' + filename);
let layerImage = new ImageLayer({
source: new Static({
url: filePath,
crossOrigin: '',
projection: 'EPSG:31982',
imageExtent: layerExtent
}),
name: "layerImage",
visible: true,
});
this.map.addLayer(layerImage);
I saw in some related questions that this can be caused by debugging with Chrome, but the same problem happens if I not use it.我在一些相关的问题中看到这可能是由 Chrome 调试引起的,但如果我不使用它也会出现同样的问题。
you can use url paramater as a function, maybe that helps.您可以将 url 参数用作函数,也许会有所帮助。
const convertFileSrc = this.win.Ionic.WebView.convertFileSrc;
let layerImage = new ImageLayer({
source: new Static({
url: (extent) => {
return convertFileSrc(this.file.externalDataDirectory + 'projects/' + this.projectId + '/layers/' + filename);
},
crossOrigin: '',
projection: 'EPSG:31982',
imageExtent: layerExtent
}),
name: "layerImage",
visible: true,
});
this.map.addLayer(layerImage);
As per @Mike comment, the problem is solved if I remove the crossOrigin: ''
option from Static
.根据@Mike 的评论,如果我从
Static
删除crossOrigin: ''
选项,问题就解决了。
let layerImage = new ImageLayer({
source: new Static({
url: filePath,
projection: 'EPSG:31982',
imageExtent: layerExtent
}),
name: "layerImage",
visible: true,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.