ImageLoader
ImageLoader
ui/res.ImageLoader
Cooperative image loading for ImageView and other views (memory + disk cache, cancellation).
Table of contents
Methods
- cancel
- clearDiskCache
- clearMemoryCache
- loadImageBitmap
- loadImageDrawable
- loadImageInto
- loadImageIntoBackground
Methods
cancel
▸ cancel(view): void
Cancel any in-flight load tagged to view. If nothing is loading, this is a no-op.
Example
"nodejs";
const { imageLoader } = require("ui/res");
imageLoader.loadImageInto(imageView, "https://example.com/image.jpg");
imageLoader.cancel(imageView);Parameters
| Name | Type | Description |
|---|---|---|
view | View | Target view to cancel. |
Returns
void
clearDiskCache
▸ clearDiskCache(): void
Delete all files in the on-disk image cache.
Example
"nodejs";
const { imageLoader } = require("ui/res");
imageLoader.clearDiskCache();
console.log("Disk cache cleared");Returns
void
clearMemoryCache
▸ clearMemoryCache(): void
Drop in-memory bitmaps. Does not remove disk cache entries.
Example
"nodejs";
const { imageLoader } = require("ui/res");
imageLoader.clearMemoryCache();
console.log("Memory cache cleared");Returns
void
loadImageBitmap
▸ loadImageBitmap(view, uri): Promise<Bitmap>
Load uri and resolve an Android Bitmap. uri may be http(s), a filesystem path, or content://….
Example
"nodejs";
const { imageLoader } = require("ui/res");
async function main() {
const bitmap = await imageLoader.loadImageBitmap(
view,
"https://example.com/image.jpg",
);
imageView.setImageBitmap(bitmap);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
view | View | Any view that supplies a Context (often an ImageView). |
uri | String | Image location. |
Returns
Promise<Bitmap>
loadImageDrawable
▸ loadImageDrawable(view, uri): Promise<Drawable>
Same as loadImageBitmap, but returns a Drawable.
Example
"nodejs";
const { imageLoader } = require("ui/res");
async function main() {
const drawable = await imageLoader.loadImageDrawable(
view,
"https://example.com/image.jpg",
);
imageView.setImageDrawable(drawable);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
view | View | Context source view. |
uri | String | Image location. |
Returns
Promise<Drawable>
loadImageInto
▸ loadImageInto(imageView, uri): Promise<void>
Decode uri and assign the result directly to imageView.
Example
"nodejs";
const { imageLoader } = require("ui/res");
const { inflateXml } = require("ui/layout");
async function main() {
const rootView = inflateXml(
context,
`
<ImageView id="img" />
`,
);
const imageView = rootView.findView("img");
await imageLoader.loadImageInto(imageView, "https://example.com/image.jpg");
await imageLoader.loadImageInto(imageView, "/sdcard/image.png");
}
main();Parameters
| Name | Type | Description |
|---|---|---|
imageView | ImageView | Destination view. |
uri | String | Image location. |
Returns
Promise<void>
Resolves when the image has been applied.
loadImageIntoBackground
▸ loadImageIntoBackground(view, uri): Promise<void>
Load uri and set it as view’s background drawable.
Example
"nodejs";
const { imageLoader } = require("ui/res");
const { inflateXml } = require("ui/layout");
async function main() {
const rootView = inflateXml(
context,
`
<LinearLayout id="container" />
`,
);
const container = rootView.findView("container");
await imageLoader.loadImageIntoBackground(
container,
"https://example.com/background.jpg",
);
}
main();Parameters
| Name | Type | Description |
|---|---|---|
view | View | Target view. |
uri | String | Image location. |
Returns
Promise<void>
Resolves when the background has been applied.
