How use display.newImage on local macOS files?

Question about using display.newImage for arbitrary jpg files on macOS …

I know I used to know this…but…
how can I get display.newImage to open an arbitrary location .jpg file on macOS?
(I.e., a file specified by the user at run-time, not a file in my application directory.)

I’m building a solar2D app to run native on macOS.

I have found (or was told) a jpg filename (potentially anywhere on the filesystem of any drive), and I want to open/view it with display.newImage ().

But…it seems like native apps cannot do that.

Instead, the only jpg files I can open with display.newImage are those specified in my app build.

I can do: io.open (filename)
…so I know I have the filename (and path) correct, and that the OS security allows access to the file!

Note: I also tried copying the user-specified file to my app’s “document directory” (which happened to be /Users/sieler/Library/Application Support/Corona Simulator/test_walkdir-E35B97E460A38FA33A8E87FD4191A4CD/Documents), and then tried display.newImage, and it still failed.

Any suggestions, please?
thanks!

You can open image files from:

  • resource folder (where your lua files live) or a subfolder of resource.
  • an arbitrary location on your machine IF that folder containing that image is linked in your resource folder or a subfolder.

I’m a Windows user (primarily) so I don’t do this, but I’m sure you can google the command to symbolic link a folder OR someone will post back with the command here.

For Windows, to create a symlink open a terminal window (or powershell) as administrator, navigate to the directory where you want to have access to the target, and then:

To create a symlink to a directory:
mklink /D link target

Sample:
mklink /D PNG "D:\1\Assets\1 Free Assets\2D\fx\Free Game FX\PNG"

To create a symlink to a file:
mklink link target

Sample:
mklink 67.png "D:\1\Assets\1 Free Assets\2D\fx\Free Game FX\PNG\67.png"

EDIT:
Whoops, didn’t realize Mac is what’s really wanted. :grinning_face_with_smiling_eyes:

For Mac, same as Windows except this is the syntax:
ln -s target link

Sample:
ln -s /Users/username/Documents/myfile.txt myfile_link
No distinction when the target is a directory or file.

1 Like

Hi…I’ll give that (symlinks) a try, I think it’s a clever approach at a workaround, thanks!

It’s a horrible thing to require (i.e., having jpgs in a known folder), of course … it completely defeats the concept of “native application”.
(E.g., one doesn’t have to create symlinks to run Photoshop, or Paintbrush, or other native Mac apps : )

Hello sieler2,

I have been using a “bytemap” plugin provided kindly by Xibalba Studios @StarCrunch, on a Windows/PC machine (in simulator and windows executable), to directly load jpeg and png images. Please see this link for documentation, Documentation. You can find an example provided by StarCrunch here, solar2d-plugins/Bytemap/Solar2D at master · ggcrunchy/solar2d-plugins · GitHub. An example snippet I used in my code is as below, which may be useful to you.

local bm = byteMap.loadTexture( { 
    filename = fullpath,
    is_absolute = true,
} )
local image = display.newImage( bm.filename, bm.baseDir )

PS> I am not sure if or how it would work on a Mac/Apple as I do not have them. Hope this helps.

Hi…great info, thanks! I’ll see if there’s a Mac version. (Eventually, I’d want the Window plugin, too!)

thanks again,

Stan

Hi @luantiang … with your help, I was able to open/display an arbitrary location jpg image (i.e., one not known at app build time, and not in my app bundle)!

The key was indeed the bytemap package.

I must admit to not understanding the internals of the set of files I got from githhub (e.g., why are there directories for android, ios, etc.), but it seems like the short-hand version of using it simply requires me to add:

   plugins = { ["plugin.Bytemap"] = { publisherId = "com.xibalbastudios", } },

in my build.settings file, doing the “require”, and then calling bytemap.loadTexture ( { filename = user_selected_filename, is_absolute = true, }.

(I’m assuming that the github package would allow me to build my own copy of the plugin, per platform … something I’ve never done.)

thanks again!

The only thing I would point out is that if you’re using Bytemap to load images and these images are meant to be used with your app then you’ll need to copy those sources to your Solar2d project’s directory and make changes to the way they are loaded before building and shipping your app.

If your use case is different then ignore my comment. :slightly_smiling_face: