Created
May 7, 2019 13:49
-
-
Save exclusiveTanim/ba1b2c7fba5142617433dcd1d26c4fb4 to your computer and use it in GitHub Desktop.
Example code which is not working for iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DoTest() { | |
//download image, then show it in a web view | |
//create directory | |
sDir = Ti.Filesystem.applicationDataDirectory + "/productimages/"; | |
d = Ti.Filesystem.getFile(sDir); | |
if (!d.exists()) { | |
d.createDirectory(); | |
} | |
sImageName = "Fortune-Cookie-2748_large.png"; | |
sURL = "https://boomboom.cart32.com/productimages/" + sImageName; | |
var xhr = Titanium.Network.createHTTPClient(); | |
xhr.onload = function() { | |
f = Ti.Filesystem.getFile(sDir + sImageName); | |
//write file | |
Ti.API.info('Status = ' + xhr.status); | |
if (xhr.status == 200) { | |
Ti.API.info('SUCCESS - Size= ' + xhr.responseData.size + ' Downloaded image(' + sImageName + ') - Writing ' + f.nativePath); | |
//write PNG file | |
f.write(xhr.responseData); | |
ShowImageInWebView(sDir + sImageName); | |
} | |
}; | |
xhr.onerror = function(e) { | |
Ti.API.info('Error: ' + e.error); | |
}; | |
Ti.API.info('Calling ' + sURL); | |
xhr.open('GET', sURL); | |
xhr.send(); | |
} | |
function ShowImageInWebView(sImageFile) { | |
var f = Ti.Filesystem.getFile(sImageFile); | |
Ti.API.info('f.nativePath = ' + f.nativePath); | |
sHTML = 'Image<br><img src="' + f.nativePath + '" />'; | |
var wv = Ti.UI.createWebView({}); | |
wv.html = sHTML; | |
var win = Titanium.UI.createWindow({ | |
title : 'Test', | |
modal : true, | |
backgroundColor : '#ff0000' | |
}); | |
win.add(wv); | |
win.open(); | |
} | |
DoTest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On device: It's [INFO] : Calling https://boomboom.cart32.com/productimages/Fortune-Cookie-2748_large.png
Then:
[INFO] : Status = 200
[INFO] : SUCCESS - Size= 390580 Downloaded image(Fortune-Cookie-2748_large.png) - Writing file:///var/mobile/Containers/Data/Application/061D8DA1-A61B-47C8-8279-F9571E19D402/Documents/productimages/Fortune-Cookie-2748_large.png
[INFO] : f.nativePath = file:///var/mobile/Containers/Data/Application/061D8DA1-A61B-47C8-8279-F9571E19D402/Documents/productimages/Fortune-Cookie-2748_large.png