Mario
Mario

Reputation: 51

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(). The code prints the blob data in android but it prints undefined in iOS. Following function is called on Button click event 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 . What am I missing here ? pls help. Thank you.

Upvotes: 0

Views: 377

Answers (1)

Rene Pot
Rene Pot

Reputation: 24815

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. 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.

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.

Upvotes: 1

Related Questions