10

In my app I had a folder of images from which I was getting the names to use as keys to a Dictionary. I have now moved all images to an asset catalog and created groups for the relevant folders. The code using FileManager is not working anymore as it can not find any folders (due to the assets being compiled into a .car file).

How should I approach this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dionysis
  • 804
  • 8
  • 25

2 Answers2

21

So (following matt's suggestion) here is what I did:

I created a RunScript build phase to run before compiling

enter image description here

and used a script to create a txt file with the names of the files that I can then use during runtime

#!/bin/sh
>./your_app_folder/fileNames.txt
for FILE in ./your_app_folder/Images.xcassets/actions/*; do
echo $FILE >> ./your_app_folder/fileNames.txt
done
Dionysis
  • 804
  • 8
  • 25
  • 3
    In 48 hours you can accept your own answer instead of mine. You should do that. I will not be at all offended! Not that that matters, but my point is, this is really a nice solution and posterity deserves to see it as the solution, which the checkmark will show. You did the work here, not me. – matt Jan 13 '15 at 20:08
  • Ah. Excellent answer indeed. It wouldn't work in my particular case as the order of the images is not the same as the alphabetical order. I do like this answer though. – Fogmeister Jan 13 '15 at 20:10
8

You have two choices:

  • Don't Do That. Go right on using a folder of images.

  • Supply the keys in some other way. If you don't want to hard-code them into the code itself, you could make a text file. But of course you will have to keep that text file updated as you add / remove images.

You'll notice that I didn't say you could magically introspect the asset catalog. That's because you can't.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    You can, there's some preliminary info [here](https://github.com/davepeck/iOS-artwork) But you absolutely should NOT even think about it. The right way is almost certainly to go back to your filesystem approach. – David Berry Jan 12 '15 at 17:42
  • @David Good info! You and I are using different meanings of the word "can" :))) – matt Jan 12 '15 at 17:57
  • @David thanks guys :) I actually went for the second option but created a run script build phase with a bash script to create the text file programmatically. This is possible since before compilation the xcassets folder is accessible as normal. – Dionysis Jan 13 '15 at 18:21
  • 2
    @Dionysis Oooh, cool. I think you should post your bash script as an answer and accept _that_ answer instead of mine. This would be really useful for other people to see! – matt Jan 13 '15 at 18:27
  • Yeah, I thought about that later and was going to recommend it. The format of the `contents.json` file is pretty trivial to parse, particularly if you already have a json parser. For that reason I probably would've done it as a python script, but either way :) – David Berry Jan 13 '15 at 18:51