我正在使用ionic 2原生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
})
现在我正在尝试使用图像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原生文件插件将图像转换为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(9.3)上如何使用Ionic 2访问本地图像呢?