ImageSource
This class encapsulates the common abstraction behind a platform specific object (typically a Bitmap for Android and a UIImage for iOS) that is used as a source for images.
Usage
Load image using resource name
This is similar to loading Bitmap from R.drawable.logo on Android or calling [UIImage imageNamed@"logo"] on iOS.
The method fromResource('name')
creates an ImageSource instance and loads it afrom the specified resource name.
Load image from a local file
Use the ImageSource.fromFile(path)
method to load an ImageSource instance from the specified file asynchronously.
Parameter(s)
Name | Type | Description |
---|---|---|
|
|
The location of the file on the file system. |
async function createImageSourceFromFile() {
const folder = knownFolders.currentApp()
const filePath = path.join(folder.path, 'images/logo.png')
folder.getFile('images/logo.png') // 1. Create the file you want to save the image to.
try {
const image = await ImageSource.fromResource('logo')
const saved = await image.saveToFileAsync(filePath, 'png') // 2. Save the image to the file created in 1
if (saved) {
const imageFromFile = await ImageSource.fromFile(filePath)
console.log('Image height: ' + imageFromFile.height)
} else {
Dialogs.alert('Not saved')
}
} catch (err) {
console.error(err)
}
}
async function createImageSourceFromFile() {
const folder: Folder = knownFolders.currentApp()
const filePath: string = path.join(folder.path, 'images/logo.png')
folder.getFile('images/logo.png') // 1. Create the file you want to save the image to.
try {
const image: ImageSource = await ImageSource.fromResource('logo')
const saved: boolean = await image.saveToFileAsync(filePath, 'png') // 2. Save the image to the file created in 1
if (saved) {
const imageFromFile: ImageSource = await ImageSource.fromFile(filePath)
console.log('Image height: ' + imageFromFile.height)
} else {
Dialogs.alert('Not saved')
}
} catch (err) {
console.error(err)
}
}
Save an image to a local file
async function saveImageToFile() {
try {
const image = await ImageSource.fromBase64(base64String)
const folderDest = knownFolders.documents()
folderDest.getFile('/images/test.png') //1. Create the file
const pathDest = path.join(folderDest.path, '/images/test.png')
const saved: boolean = await image.saveToFileAsync(pathDest, 'png') // Save to the file
if (saved) {
Dialogs.alert('Saved successfully')
}
} catch (err) {
Dialogs.alert(err)
}
}
async function saveImageToFile() {
try {
const image: ImageSource = await ImageSource.fromBase64(base64String)
const folderDest: Folder = knownFolders.documents()
folderDest.getFile('/images/test.png') //1. Create the file
const pathDest = path.join(folderDest.path, '/images/test.png')
const saved: boolean = await image.saveToFileAsync(pathDest, 'png') // Save to the file
if (saved) {
Dialogs.alert('Saved successfully')
}
} catch (err) {
Dialogs.alert(err)
}
}
Create image from base64 string
Use ImageSource.fromBase64(source)
method to asynchronously load an image instance from the specified base64 encoded string.
Parameter(s)
Name | Type | Description |
---|---|---|
|
|
The Base64 string to load the image from. |
async function createImageFromBase64() {
try {
const image = await ImageSource.fromBase64(base64String)
if (isIOS) {
// isIOS must be imported,from @nativescript/core
console.log(image.ios) // <UIImage:0x280a5e640 anonymous {1024, 1024}>
} else {
console.log(image.android) // android.graphics.Bitmap@9d09ef6
}
} catch (err) {}
}
async function createImage() {
try {
const image: ImageSource = await ImageSource.fromBase64(base64String)
if (isIOS) {
// isIOS must be imported,from @nativescript/core
console.log(image.ios) // <UIImage:0x280a5e640 anonymous {1024, 1024} >
} else {
console.log(image.android) // android.graphics.Bitmap@9d09ef6
}
} catch (err) {}
}
Properties
Name | Type | Description |
---|---|---|
|
|
Gets the height of this instance. This is a read-only property. |
|
|
Gets the width of this instance. This is a read-only property. |
|
|
Gets or sets the rotation angle that should be applied to the image.
( |
|
|
The iOS-specific UIImage instance. Will be undefined when running on Android. |
|
|
The Android-specific image instance. Will be undefined when running on iOS. |
ImageSource Static Methods
Name | Return Type | Description |
---|---|---|
|
|
Loads an image instance from the specified asset asynchronously. |
|
|
Downloads the image from the provided Url and creates a new ImageSource instance from it. |
|
|
Loads an image instance from the specified resource name. |
|
|
Loads an image instance from the specified resource name asynchronously. |
|
|
Loads an image instance from the specified file on the file system. |
|
|
Asynchronously loads an image instance from the specified file on the file system. |
|
|
Creates a new ImageSource instance and loads it from the specified local file or resource (if specified with the "res://" prefix). |
|
|
Loads an image instance from the specified native image data.
|
|
|
Loads an image instance from the specified base64 encoded string. |
|
|
Asynchronously loads an image instance from the specified base64 encoded string. |
|
|
Creates a new ImageSource instance and loads it from the specified font icon code.
+ |
ImageSource instance methods
Name | Return Type | Description |
---|---|---|
|
Creates a new ImageSource instance and sets the provided native source object (typically a Bitmap for Android or UIImage for iOS). The native source object will update either the android or ios properties, depending on the target os. |
|
|
|
Sets the provided native source object (typically a Bitmap or a UIImage). This will update either the android or ios properties, depending on the target os. |
|
|
Saves this instance to the specified file, using the provided image format and quality. |
|
|
Asynchronously saves this instance to the specified file on the file system, using the provided image |
|
|
Converts the image to base64 encoded string, using the provided image |
|
|
Asynchronously converts the image to base64 encoded string, using the provided image |
|
|
Returns a new |
|
|
Similar to |