let fm = FileManager.iCloud();
let path = fm.documentsDirectory() + "/<your image name>.jpg";
// Image.fromFile(path) can also be used
w.backgroundImage = fm.readImage(path);
This way you don’t have to memorize the absolute path. And the path could also be something else on a different device.
I’m also trying to put an image in the background of a widget and I’m not succeeding either. I tried your script:
let fm = FileManager.iCloud()
let path = fm.documentsDirectory() + "/widget_background.jpg"
widget.backgroundImage = fm.readImage(path)
But the image is not displayed and here’s what the console returns to me:
2020-10-25 10:43:08: widget_background.jpg is stored in iCloud but the file have not been downloaded. Use downloadFileFromiCloud(filePath) on a FileManager to download the file before accessing it.
2020-10-25 10:43:08: /private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/widget_background.jpg
It’s all in the error message: the file is not downloaded yet. You can either wait until that happens or use the Scriptable method to download it explicitly.
This is done with the method FileManager.downloadFileFromiCloud(filepath) as described in the error message.
So the complete example would be
let fm = FileManager.iCloud();
let path = fm.documentsDirectory() + "/<your image name>.jpg";
await fm.downloadFileFromiCloud(path);
// Image.fromFile(path) can also be used
w.backgroundImage = fm.readImage(path);
// "w" is your widget instance