Hi, I’m new to Figma Plugin, and I want to get the image filles from user selection.
Here’s my code’s logic.
- Get a parameter which is a selection
- Check if the selection.fills[0].type is “IMAGE”
- Get imageHash from it and call Figma.getImageByHash(imageHash)
- call getSizeAsync() and getBytesAsync() methods
Right after step 3, I can see that I got an image object-not null-so I think I get a right image.
But If I call getSizeAsync() or getBytesAsync(), an error occurs.
If I call getSizeAsync(), the error message is,
Image dimensions not available
If I call getBytesAsync(), the error message is,
Uncaught (in promise) Error: This VM has been destroyed
I couldn’t find about these errors in internet or forum at all.
Here’s my code.
if (
selection.type === "RECTANGLE" &&
Array.isArray(selection.fills) &&
selection.fills[0].type === "IMAGE"
) {
const { imageHash } = selection.fills[0];
if (!imageHash) {
// If no imageHash is available.
const message = "Please check if you selected images only.";
throw new Error(message);
}
const image = figma.getImageByHash(imageHash);
if (!image) {
// If no imageHash is available.
const message = "Failed to load an image from your selection.";
throw new Error(message);
}
console.log(image);
const { width, height } = await image.getSizeAsync();
// const imageData = await image.getBytesAsync();
Would you share your insights please?
Any information about this would be really helpful, thanks.