Widget Background Image

Sorry for noob question.

I am trying to create a backgroundimage for a simple ios widget.

I have copied the jpg file to both local ICloud and local files but can’t seem to get the correct code to reference it.

Do I need FileManager to get the path and then what is correct syntax to load it into the widget and the background?

thanks for any help

This is how I add my backgrounds

let widget = new ListWidget();
widget.setPadding(0, 0, 0, 0)

let req = new Request (“https://i.imgur.com/abc.jpg”)

let image = await req.loadImage()

widget.backgroundImage = image

Thanks Justin.

Your solution is for a image at an URL enpoint.

Figured out how to old it locally by hard coding the path that I found elsewhere for local icloud, namely:

Path = “/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/IMG_nnnn.JPG” and then w.backgroundImage = Image.fromFile(path)

thanks anyway

The more correct way would be:

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.

2 Likes

Thanks @schl3ck!

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

Any help? TIA

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.

Hi @chrillek,

Thanks for your reply.

Sorry but, no matter how long I wait, the picture doesn’t load. What’s the method to download it explicitly from iCloud?

I manage to do this with an image coming from an external server but not from iCloud. Weird!

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
2 Likes

Thanks @schl3ck! This is working fine now. I tried the downloadFileFromiCloud method but I missed the await operator. Thanks again!

Hi

Is there a way to do it offline and without iCloud? Because I want it to run offline and also my iCloud storage is already full.

When I tried with FileManager.local() it never finds the file and says it doesn’t exist.

Please reply