Nativescript: ImageSource fromFile() is deprecated also error fromFileSync

Created on 18 Jan 2020  Â·  3Comments  Â·  Source: NativeScript/NativeScript

Environment
LINUX OS (DEB)
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

✔ Getting NativeScript components versions information...
✔ Component nativescript has 6.3.3 version and is up to date.
✔ Component tns-core-modules has 6.3.2 version and is up to date.
✔ Component tns-android has 6.3.1 version and is up to date. # testing on emulator

*Function deprecated error *
While using saving image got error warning,
JS: 'fromFile() is deprecated. Use ImageSource.fromFileSync() instead.'

when i fixed as per then give fatal error

JS: 'Error -> imageSourceModule.fromFileSync is not a function'

How to fix this ?

needs more info

Most helpful comment

@akwatra the thing is that the new fromFileSync is a static method of ImageSource class. So that said you need to call it via the ImageSource class itself.

Here is a TypeScript example to make things more clear

import { fromFile, ImageSource } from "tns-core-modules/image-source";

const imgWithDeprecatedMethod = fromFile(path);
const img = ImageSource.fromFileSync(path);

And the same in plain JavaScript

let fromFile = require("tns-core-modules/image-source").fromFile; // deprecated method
let ImageSource = require("tns-core-modules/image-source").ImageSource;

let imgWithDeprecatedMethod = fromFile(path);
let img = ImageSource.fromFileSync(path);

All 3 comments

@akwatra can you give a code snippet or demo project that demonstrates your case?

I guess you can test with "tns-core-modules/image-source"
its reference from there

  saveImage(imageAsset){

    const img = imageSourceModule.fromFile(imageAsset._android);
    const folderDest = fileSystemModule.knownFolders.documents();
    const pathDest = fileSystemModule.path.join(folderDest.path, "app/images/" + "_test.png");

    const saved = img.saveToFile(pathDest, "png");
    if (saved) {
        console.log("Image saved successfully!");
         that.imgBill = pathDest;
    }          

 }

@akwatra the thing is that the new fromFileSync is a static method of ImageSource class. So that said you need to call it via the ImageSource class itself.

Here is a TypeScript example to make things more clear

import { fromFile, ImageSource } from "tns-core-modules/image-source";

const imgWithDeprecatedMethod = fromFile(path);
const img = ImageSource.fromFileSync(path);

And the same in plain JavaScript

let fromFile = require("tns-core-modules/image-source").fromFile; // deprecated method
let ImageSource = require("tns-core-modules/image-source").ImageSource;

let imgWithDeprecatedMethod = fromFile(path);
let img = ImageSource.fromFileSync(path);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kn9ts picture kn9ts  Â·  3Comments

rogangriffin picture rogangriffin  Â·  3Comments

Leo-lay picture Leo-lay  Â·  3Comments

guillaume-roy picture guillaume-roy  Â·  3Comments

OscarLopezArnaiz picture OscarLopezArnaiz  Â·  3Comments