iosrubyxcodexcodeproj

Create file reference to image in Images.xcassets using xcodeproj


I'm writing a script with Xcodeproj and I would like to create references to image files inside .xcassets. Finder treats .xcassets & .imageset as Folders but I don't want to create a group in xcode for each of those folders.

I'm able to create a reference when I know the containing groups in Xcode. i.e. project.main_group['Src'].new_reference(pathToProject/Src/fileName.m)

I'd like to iterate through the sub-directories and files of .xcassets and create a reference for each image. But I don't want to create a group in Xcode that matches each imageset in Finder because Xcode deals with the .xcassets folder in its own way.

I would like to be able to do something like this:

project.main_group['Src']['Images.xcassets']['image_name.imageset'] .new_reference(pathToProject/Src/Images.xcassets/image_name.imageset/image_name.png) The above line doesn't work using Xcodeproj because it can't find any Groups named Images.xcassets or image_name.imageset.

Can anyone point me in the right direction or give an example of how I would create a reference to an image file stored inside Images.xcassets?

Thanks for your time!


Solution

  • It turns out Xcode handles the image references for you if you add an .xcassets file to your project.

    Another thing I learned is that the .xcassets folder should be added to your target(s) as a resource with xcodeproj instead of a file reference. That way it will correctly show up under Project -> Target -> Build Phases -> Copy Bundle Resources.

    e.g. target.add_resources([file_reference_to_xcassets]) instead of target.add_file_references([file_reference_to_xcassets])

    Hopefully, this helps someone down the road :)