Image source

Using the image source requires the image-source module.

// 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:

// var fs = require("file-system");

Loading and saving images

Load image using resource name

This is similar to loading Bitmap from R.drawable.logo on Android or calling [UIImage imageNamed@"logo"] on iOS

var img = imageSource.fromResource("logo");

Load image from URL

imageSource.fromUrl("https://www.google.com/images/errors/logo_sm_2.png")
    .then(function (res: imageSource.ImageSource) {
    //console.log("Image successfully loaded");
}, function (error) {
        //console.log("Error loading image: " + error);
    });

Save image source to PNG or JPG file

var img = imageSource.fromFile(imagePath);
var folder = fs.knownFolders.documents();
var path = fs.path.join(folder.path, "Test.png");
var saved = img.saveToFile(path, enums.ImageFormat.png);

Load image from a local file

var folder = fs.knownFolders.documents();
var path = fs.path.join(folder.path, "Test.png");
var img = imageSource.fromFile(path);

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© 2016 Progress Software Corporation. All Rights Reserved.