首页 文章

在iOS上使用Ionic 2 Native访问本 Map 像

提问于
浏览
9

我正在使用ionic 2 native ImagePicker插件

ImagePicker.getPictures({
  maximumImagesCount: 1
}).then((results) => {
    vm.sourcePath = results[0]
    console.log(vm.sourcePath)
    // file:///Users/aymanjitan/Library/Developer/CoreSimulator/Devices/E937952F-7FAA-4DBB-8E23-80B04F3B6C72/data/Containers/Data/Application/FCF9097F-67BD-4DAD-AE30-D2F1839C0374/tmp/cdv_photo_003.jpg
})

现在我尝试使用image src属性访问此图像

<img [src]='sourcePath' />

甚至硬编码路径

<img src="file:///Users/aymanjitan/Library/Developer/CoreSimulator/Devices/E937952F-7FAA-4DBB-8E23-80B04F3B6C72/data/Containers/Data/Application/FCF9097F-67BD-4DAD-AE30-D2F1839C0374/tmp/cdv_photo_003.jpg"

但这没有任何表现 .

知道

<apan>{{sourcePath}}</apan>

正确显示路径!

我尝试使用ionic native File plugin将图像转换为base64

var sourceDirectory = vm.sourcePath.substring(0, vm.sourcePath.lastIndexOf('/') + 1);
var sourceFileName = vm.sourcePath.substring(vm.sourcePath.lastIndexOf('/') + 1, vm.sourcePath.length);
File.readAsArrayBuffer(sourceDirectory, sourceFileName).then((fileData)=> {
  console.log(fileData)
}, () => {
  console.log('error')
})

但是这会引发错误

我在config.xml中添加了这些白名单规则

<access origin="*"/>
<allow-navigation href="*"/>
<allow-navigation href="file://*/*"/>

但仍然没有运气

我虽然可能返回的文件路径不正确所以我把它放在我的浏览器中,它显示了应该选择的图像

那么如何在iOS上使用ionic 2访问本 Map 像(9.3)

1 回答

  • 3

    linkauthor建议的解决方案的解决方法可以完成

    使用 $cordovaFile.readAsDataUrl(„file:///...“, „myPic.png“) ,您可以请求文件的内容 .

    View <img src=“{{ imgSrc }}“ />

    controller

    $cordovaFile.readAsDataUrl(„file:///...“, „myPic.png“).then(
    function(res) { $scope.imgSrc = res; },
    function(error) { alert(damn!‘); }
    );
    

    并在 config.xml

    <allow-navigation href="*"/>
    <allow-navigation href="file://*/*" />
    <allow-intent href="*"/>
    <access origin="*"/>
    

    接下来是你的 index.html 文件

    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    

    From Cordova 4.0.0 for Android's update:

    白名单功能已更新您将需要添加新的cordova-plugin-whitelist插件以继续使用白名单现在支持设置内容 - 安全策略(CSP)并且是白名单的推荐方式(请参阅插件自述文件中的详细信息)默认情况下,网络请求在没有插件的情况下被阻止,因此安装此插件甚至允许所有请求,即使您使用的是CSP . 此新白名单已增强为更安全和可配置,但旧版白名单行为仍可通过单独的插件获得(不推荐) .

    注意:虽然不是此版本的严格部分,但cordova-cli创建的最新默认应用程序默认包含此插件 .

    填充归属归于此thread以及链接和上面提到的作者

相关问题