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("icon");

Load image from URL

    .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, "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);

Save image source from imageAsset to PNG file

import * as fs from "tns-core-modules/file-system";

export function imageSourceFromAsset(imageAsset){
    let source = new imageSource.ImageSource();
     source.fromAsset(imageAsset).then((source) => {
         let folder = fs.knownFolders.documents().path;
         let fileName = "Test.png"
         let path = fs.path.join(folder, fileName);
         let saved = source.saveToFile(path, "png");
            console.log("saved image")

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

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