// // # Image source // Using the image source requires the image-source module. // ``` JavaScript //var imageSource = require("image-source"); // ``` // The pre-required `imageSource` module is used throughout the following code snippets. // We also use fs module defined as follows: // ``` JavaScript //var fs = require("file-system"); // ``` // ## Loading and saving images // import imageSource = require("image-source/image-source"); import fs = require("file-system/file-system"); import app = require("application/application"); import TKUnit = require("Tests/TKUnit"); export var testFromResource = function () { // // ### Load image using resource name // This is similar to loading Bitmap from `R.drawable.logo` on Android or calling `[UIImage imageNamed@"logo"]` on iOS // ``` JavaScript var img = imageSource.fromResource("logo"); // ``` // TKUnit.assert(img.height > 0, "image.fromResource failed"); } export var testFromUrl = function () { var completed; var result: imageSource.ImageSource; // // ### Load image from URL // ``` JavaScript imageSource.fromUrl("http://www.google.com/images/errors/logo_sm_2.png") .then(function (res: imageSource.ImageSource) { //console.log("Image successfully loaded"); // completed = true; result = res; // }) .fail(function (error) { //console.log("Error loading image: " + error); // completed = true; // }); // ``` // var isReady = function () { return completed; } TKUnit.waitUntilReady(isReady, 3); TKUnit.assert(typeof result !== "undefined", "Image not downloaded"); TKUnit.assert(result.height > 0, "Image not downloaded"); } export var testSaveToFile = function () { // // ### Save image source to PNG or JPG file // ``` JavaScript var img = imageSource.fromResource("logo"); var folder = fs.knownFolders.documents(); var path = fs.path.join(folder.path, "Test.png"); var saved = img.saveToFile(path, imageSource.ImageFormat.PNG); // ``` // TKUnit.assert(saved, "Image not saved to file"); TKUnit.assert(fs.File.exists(path), "Image not saved to file"); } export var testFromFile = function () { // // ### Load image from a local file // ``` JavaScript var folder = fs.knownFolders.documents(); var path = fs.path.join(folder.path, "Test.png"); var img = imageSource.fromFile(path); // ``` // TKUnit.assert(img.height > 0, "image.fromResource failed"); // remove the image from the file system var file = folder.getFile("Test.png"); file.remove(); TKUnit.assert(!fs.File.exists(path), "Test.png not removed"); } export var testNativeFields = function () { var img = imageSource.fromResource("logo"); if (app.android) { TKUnit.assert(img.android != null, "Image.android not updated."); } else if (app.ios) { TKUnit.assert(img.ios != null, "Image.ios not updated."); } }