Attempting to save image to applicationDataDirectory

You must Login before you can answer or comment on any questions.

TiSDK 1.7.1 iOS 5.0

Hi all,

I'm attempting to take the URL of an image that is passed to the app and store it in applicationDataDirectory.

I've written a function that takes the url, creates an ImageView, passes it to .toImage() and then writes it to a file.

http://pastie.org/3064770

When I run it in the simulator and go to the applicationDataDirectory to view the image it's the generic placeholder image.

Thoughts?

2 Answers

Hi, I'am realize this with the following code:

var cachedImageView = function(imageDirectoryName, url, imageViewObject, hires) {
    var filename = url.split('/');
    var hiresfilename;
    filename = filename[filename.length - 1];
    hiresfilename = filename.split('.');
    hiresfilename = hiresfilename[hiresfilename.length - 2] + '@2x' + hiresfilename[hiresfilename.length - 1];
    var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, filename);
    var hiresfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, hiresfilename);
 
    if (file.exists()) {
        imageViewObject.image = file.nativePath;
    } else {
        var g = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName);
        if (!g.exists()) {
            g.createDirectory();
    };
    var xhr = Ti.Network.createHTTPClient();
    xhr.onload = function() {
        if (xhr.status == 200) {
            file.write(xhr.responseData);
            if (hires) {
                hiresfile.write(xhr.responseData);
            }   
            imageViewObject.image = file.nativePath;
        };
    };
    xhr.open('GET', url);
    xhr.send();
};
This code realizes also hires images.

Rainer

Hey, Check this link,

http://developer.appcelerator.com/question/45471/how-to-save-a-remote-imagepicture-to-database-or-filesystem

Dan Tamas has given a very good function for this purpose.

Your Answer

Think you can help? Login to answer this question!