Create file reference to image in Images.xcassets using xcodeproj - ios

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!

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 :)

Related

xcassets import a folder of images by creating folder references and not actual copy

I'm trying to import a folder which contains hundred of images to xcassets.
By importing it normally, the problem is that all the actual images are copied (Contents.json file is also generated for each one of them).
I would like them to not be copied but be referenced, similarly to the way we can do so in the project folder by pressing "Add files to ProjectName" and checking the "Create folder references" option.
Is there any way to achieve that?
(In fact I want to use a common assets folder for android and ios in a react-native project, that's why I need this. For android, I did found how to link it to the drawable folder through build.gradle, but for ios not yet)

Xcode -using two xcassets how would i override

Let me explain the architecture i'd like to build in xCode. here is a photo of xcassets i have created in the project structure:
From the image you can see that i have created two xcassets folders. The first one called images.xcassets hosts all images that are common to both my targets.Its membership is in both target1 and target2. This xcassets sheet is filled with many images which i inherited from a fork.
The images-override.xcassets folder on the other hand, is where i'd like to put any images that ARE NOT common to both my targets. So the target membership for this directory should be only target2.
What im trying to accomplish:
Doing it this way, if i attempt to retrieve an image called myImage.png it should first check the images-override to see if any image is overriden, if its not found there it should then check images.xcassets for the image.
So my question is about what order the images will get searched for. I'd like the project to first check the images-overriden.xcassets sheet then check (if necessary) the images.xcassets folder. Any ideas ?
In general, you cannot include multiple assets with the same name in Xcode. For your purpose, I'd recommend a naming convention:
filename.png
filename-override.png
For example. When loading your image, you first check if filename-override.png exists, if so load it. If not, load filename.png.

Accidentally removed xcassets file from Xcode project

I wanted to remove a single folder from the image collection, however, I somehow managed to throw the entire xcassets file into the trash.
The normal 'Put back' method is not available, and simply dragging the deleted folder to Xcode yields an error:
"Images.xcassets" couldn't be copied to "ProjectPear" because an item with the same name already exists.
To save the file, either provide a different name, or move aside or
delete the existing file, and try again.
I'm not sure where this collection is located within my Xcode project, as the navigator doesn't appear to show anything, and the command-line isn't dropping any hints either. I'm afraid to mess around with the project.pbxproj file, as it might jumble up a lot more than just my image collection.
Edit:
Dragging the files back to Finder from Trash was successful, however, the Xcode navigator no longer recognises - or in this case, still doesn't recognise - the .xcassets file. I'm not sure whether to add something in the .pbxproj file.
I just did this myself and figured out how to quickly remedy the issue.
Go to the project folder in the Finder. You should find that the
folder Images.xcassets is still there.
Move it to a separate location like to the Desktop or something. It
still has all of the images and json files.
Jump back to Xcode and create a new asset catalog for images.
File > New > File... > Resource > Asset Catalog
Name it Images.
Quit Xcode and return to the Finder.
Copy the folders from within your old Images.xcassets to the new
Images.xcassets directory in your project.
Launch Xcode and you're back in business!
If you didn't delete it completely, you can just drag it back in from Finder. (Right click your project and select 'Reveal in Finder' to check)
If it's gone completely you simply go Cmd+N -> Resource and select Asset Catalog. You then re-add your imagages via drag and drop or right-click and 'Add Files to ...' and should be good to go.
Hope that helps.
Usually the file's reference is removed from Xcode, the original folder still exists in the project directory.
Right-click on your project folder, click "Add files to "your project"", find Assets in your project and click Add and you're
done
Had the same problem today with Xcode Beta, I also couldn't create a new assets catalog for some reason.
This was on a Watchkit Extension Project
Here's what I did to solve it:
In finder I copied an existing images.xcassets from another folder in my app to my desktop.
Removed the content of this folder.
Copied a deleted (single) Image Asset from my trash into the folder (on my desktop).
Dragged the entire folder into my project in XCode.
That fixed it and everything went back to normal.
If you have deleted it completely, then also these are the steps to get a new Images.xcassets:
In the Navigator, click on the project.
Go to : File>New>File> Add Asset Catalog> Name it.
To add AppIcon and LaunchImage in it:
Go to the Project Setting:
In General Tab, Slide down to App Icons and Launch Images
Click option next to App Icons Source and select Use Asset Catalogs
A window will pop up- select the name of your new Images.xcassets file and click on Migrate.
Select Also add LaunchImage. Now you can see the new Images.xcassets in your project :).
I accidentally deleted the reference to the asset folder so that it was no longer visible in Xcode. But when I checked from the finder, the asset folder was still in the project folder.
If you did that, just open the file menu in Xcode (from within your current project) and at the very top you can add (existing) files to that very project (back again) - it says "Add files to..."
Right click on anything in your project and click "Show in Finder". Search around in your project folder in the finder for a bit and you'll find it. I'm not on a Mac right now, or I'd give you a more direct path straight to the default .xcassets folder location.
And anyway, if you're not able to copy/create a "new" folder in that location because "an item with the same name already exists", then that means you didn't actually delete the folder. You just deleted your project's references to that folder in XCode. The folder on your hard drive didn't go anywhere.
[I realize this is a very old question from you #Aeveus]
All the answers talk about re-doing the adding assets again. Given you could not undo, the one step that is missing in your original description is this:
In your project.pbxproj file, there would have been references to assets as below:
45AE4E781C183FBA0051674F /* Assets.xcassets in Resources */ = {...
Assuming you have source code control system (git, etc) you can revert to the original contents of your project.pbxproj file, or manually just re-add the deleted lines, and re-open the project file in xcode. You are all set now.
I had the same problem. Open your XCode Project. Just go to File -> Add Files to...(your Project) -> Assets.xcassets and you are good to go!
I had same problem and I have followed the steps above from Jason, but finally i got Image.xassets in red color, so the in order to find my images I had to click in Image.xassets and then on the top right corner put the absolute path to my image.xassets folder and it fixed my problem.
Hope it helps!

Xcode: Multiple directories and subdirectories

I'm trying to figure out how to get all of my map tiles into XCode, so that I can call them like:
/0/0/0.png
/1/0/1.png
... etc
Hasn't seemed to work so far, so I was wondering if anyone could advise on how to do so.
When adding resources to Xcode, if you select "create folder references for any added folder", then Xcode will preserve hierarchic structure when copying these folders to bundle. Referenced folders have blue folder icons in project navigator.
A downside to this approach is that Xcode doesn't watch changes of files in referenced folders, so if you change an already added file, you'll have to do a clean build or the app bundle will have an old version of the file.
When you build your XCode project, all resources are copied directly to your application's bundle, so if you have several images named the same (even if they are in different directories), only one will be taken and the rest will be ignored / overwritten.
If you have something like 0/0/0.png, 1/0/0.png I'm sorry for what I have to tell you, but you will have to rename your tiles to something like 000.png, 100.png and so on.
Also, to get an image that you added in your project, you get it by name, like this:
UIImage *tileImage = [UIImage imageNamed:#"000.png"];

ios - What is the correct way to add outside files to my new project?

I have about 20 image files which I would like to a new project. I tried to make my code base clean so I made a group src/img where I intended to place the images.
Then I tried to copy the images via the file system, and obviously the groups that I made did not also create folders. So before I do anything that would dirty-up the code base, I wanted to ask here.
What is the correct way to add the files I want to add so they would render in my group under proj_name/src/img ?
Thanks!
You can first create a folder src/img inside your project in filesystem and copy all of your images in that folder. Then drag the whole img folder to your project in Xcode, but make sure you don't check copy files option when you drag. But, do select the option to create group so that image folder dragged is also a group in Xcode
The filesystem location is not represented by Xcode project groups.
The groups you are creating are supposed to be organized by you, but Xcode also provides useful options like automatically creating groups for the folders checkbox when you are adding new files to the project.
After the files are added to the project, you are free to "move" them to other groups or rename groups, that changes will be stored at the project bundle. Of course if you make the filesystem changes you'll have to remove the broken references from the project and recreate them.

Resources