Using XCAssets Properly (adding assets) - ios

I had two people discuss two different approaches with me on how to add assets such as images to a project in Swift.
Person 1: "You should create a new folder within your actual project workspace and then add all of your assets to it directly (so it's within the project). Then drag & drop the assets from this project folder to XCAssets, to actually create the icons you will use."
Person 2: "Just download your images from wherever, and drag and drop them directly. They can be from different locations. When you deploy to ITunesConnect it will copy the images."
Now I'm confused - when I drag and drop an image (let's say from my Downloads folder) doesn't Xcode copy the image locally into its own private location? Once I drag it into XCAssets, does it need that source location anymore? I thought maybe approach #1 would end up forcing Xcode to store duplicate images/assets and take up more size on the app.
What is the "correct" approach to adding assets?
Thanks!

Both actually can work (drag and drop/use asset folder). But the latest method introduced by Apple by xcasset folder. XCAsset folder is superior in the way that you can see a list of assets nicely one by one, even you have multiple sizes on it (.png #2x.png #3x.png etc). Easier to manage and see which size you are missing etc.

Related

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.

Can I access an xcassets directory on the filesystem?

I would like to dynamically load all images in an xcassets directory. The files are named StockPhoto# where # is the number in the list. If I can access my StockPhotos.xcassets at runtime to count all the files in the directory, I won't have to manually load the files each time I add new stock photos.
If there are other solutions to this problem, I'm open to that but I'm also just very curious how xcassets are handled by the file system- whether they're just reference to a set of files, or actually their own directory. Information on this is sparse.
If there are other solutions to this problem, I'm open to that
The problem is that there is no introspection at runtime into an asset catalog: it isn't a "thing" you can "see" as far as Objective-C and Cocoa Touch are concerned.
The usual solution to this kind of problem is to drag a folder full of images into your project at the outset, and when you do, choose "Create folder references for any added folders" in the dialog - not "Create groups for any added folders". The result is that the folder is copied into your app bundle, and now you can use ordinary file system methods to say "every file in this folder".
Upon compilation of your iOS project, xcassets are compiled to produce either image files, or a proprietary .car file. In that latter case images won't be stored in a directory you can browse.
If your "Deployment Target" is less that iOS7 (meaning that your app would still be able to run on iOS6)
It will produces the same set of image files that you would have had to produce without using Assets Catalog, namely <YourImageName>.png, <YourImageName>#2x.png, <YourImageName>~ipad.png, <YourImageName>~ipad#2x.png and so on, for each image set of your xcassets.
If your "Deployment Target" is iOS7 or greater (meaning that your app would only be able to run on iOS7+)
It will produce a single big .car file in the final bundle (I don't really looked up if this file was actually an sqlite3 datatbase or some proprietary format or whatnot, but who cares, you are not supposed to manipulate it anyway). This big .car file contains all the images, with all their provided variants, and even with slicing info (if you did slice some of them for tiling or to use them as 9-patch images using the tool provided for that in the Assets Catalog editor)
Whatever the produced result you shouldn't / are not supposed to dig into internal details of your bundle like that. The format of the .car file may even change from one iOS version to another (who knows? that's internal details after all which we shouldn't have to deal with) so don't base your logic on it.
[EDIT]: If you need to be sure to have a directory with your set of images at the end of the compilation, you could instead use a folder reference (referencing a real folder in the Finder, as opposed to an Xcode "group" as only group files in Xcode's Project Navigator) then use code to browse it. But then you will have to deal with other details, like only browse files that match the current device (iPhone vs. iPad, non-retina vs. retina…), so this would only shift the problem further in your case; you really should use a constant somewhere to declare the number of images (or put this in some PLIST file for example) and iterate thru them.
As the files you provide at compile time will be in your Bundle — which cannot be altered once compiled as it is digitally signed — the number of images will never changed once the app is compiled anyway. (That's not like if you used the Documents directory and enabled iTunes File Sharing or whatever, letting the user add images himself ;-))
If you're targeting iOS 7+ then no. Xcode will package the files into a proprietary format (.car) that you can't access directly.
Either use imageNamed: methods, or don't use Image Catalogs for the files you need to access directly.
as #AliSoftware suggests you can store all assets images to plist and access them later for more details see here

One Project For Multiple Applications in XCode

I am trying to use the approach of having a single project that uses different targets with different plist files. However, each target has a different set of launch and icon images. I cant however use a duplicate set of these since adding a duplicate image when the same name gives the error that its already in use. Is there anyway around this in xcode?
To save the file, either provide a different name, or move aside or delete the existing file, and try again.
You should use Xcode 5's assets catalogs. You can define a different assets catalog for each target, and in each assets catalog you can define different launch images. Names are handled by the content generator.
You can mix asset catalogs, so you can have, for example, one shared catalog for shared images, and individual ones for each different target, where you can have distinct icons and launch images.
OK, so I have changed my approach and found the easiest and a simple solution which I ignored before. All you have to do is keep the file names same but keep the files in different folders. And when adding them to project, check the target to which the files belong to and uncheck other targets. See the image.

Managing visual assets on xcode (reference, copy or both)?

Good evening overflowers,
I was wondering what would be the best way to manage assets (visual, sound, etc.) in xcode for ios or mac os projects.
I am currently working on a project that the image files change all the time (design is under construction still) and I would like to know the best way to keep all the files updated while having them in the project's directory.
So for instance I have a file named "ic_action_gray_button#2x.png" This button changes on the design but still has the same name. Is there a magical way to mirror the changes and have it also under source control without spending time finding and replacing the file all the time?
It is something easy when it comes to 50 assets but when these assets become 100 or 500 then, it becomes difficult to keep up with the changes.
Any tutorial or any guide to help me through this process?
If my question is not clear, be easy on me, I am not a native English speaker :)
Kind Regards,
Sonic555gr
You can create a separate folder for your assets inside your project folder. Then you can drag your asset folder to your project in Xcode, select "Create folder references for any added folders" and uncheck "Copy items into destination group's folder (if needed)". This should serve your purpose.
The way I do it is just to take the 100 - 150 or however many assets and paste them in Finder.
As long as the files have the same names, you are just doing an overwrite in Finder, inside the folder of your XCode project.
Any new images, you can add to XCode as usual, by drag & drop, then selecting copy and choosing the target. Any replacements can be done in Finder after that.
Git will also detect the file modifications and you can push the changes to your repo. For file/assets replacements, there is no need to do it through XCode and add the new images again.
If you are using asset catalogs for your images, its just a matter of replacing the files inside the specific folders, which is tedious, its then easier to just select them all inside XCode, delete them from the catalog, and from the project, and then just drag and drop them into the asset catalog again.

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