简体   繁体   English

无法读取Appcelerator Titanium中的图像文件

[英]Cannot read image file in appcelerator titanium

I have an image stored at /assets in my project folder and I am trying to read it using Ti.Filesystem.getFile(). 我在项目文件夹中的/ assets中存储了一个图像,并且尝试使用Ti.Filesystem.getFile()读取它。 The code prints the blob data in android but it prints undefined in iOS. 该代码在android中打印blob数据,但在iOS中打印未定义的数据。 Following function is called on Button click event https://pastebin.com/QgqLQPyz 在按钮单击事件https://pastebin.com/QgqLQPyz上调用以下功能

function readImg(e) {
var localPath = '/butterfly.jpg';
var cachedFilename = Ti.Utils.sha1(localPath) + localPath.substr(localPath.lastIndexOf('.'));
console.log("cachedFilename:---"+cachedFilename);
var cachedFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationCacheDirectory, cachedFilename);

if(!cachedFile.exists()){
    var blob = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, localPath).read();
    console.log("-----------blob in not exists:"+JSON.stringify(blob));
}

} }

When the same image path is set in ImageView it gets displayed so the issue is not with path . 当在ImageView中设置相同的图像路径时,它将显示出来,因此问题不在于path。 What am I missing here ? 我在这里想念什么? pls help. 请帮助。 Thank you. 谢谢。

The best option so you don't have to do anything specific to the images at all (keep in mind, image manipulation is pretty heavy to do on runtime), is to use a module that was made for this purpose, av.imageview . 最好的选择是使用一个模块av.imageview ,这样您根本不必执行特定于图像的任何操作(请记住,在运行时图像处理非常繁重)。 This allows you to configure all kinds of content modes. 这使您可以配置各种内容模式。

An option to get your code to work is to get the blob using the the getAsset method . 使代码正常工作的一种选择是使用getAsset方法获取blob。

var blob = Ti.Filesystem.getAsset('/images/butterfly.jpg');

and then resize where you see fit. 然后调整您认为合适的尺寸。 But I would advise you, if you do need to do this every time the app runs, then just resize once and store that resized image to be used after that. 但是我建议您,如果您确实需要在每次运行该应用程序时执行此操作,则只需调整一次大小并存储该大小调整后的图像以供以后使用。 That way there will only be a single time the image needs resizing. 这样,只有一次图像需要调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM