Thursday, 29 May 2014

Asset Catalogs


 

What is Asset Catalog?

Use asset catalogs to simplify management of images that are used by your app as part of its user interface.
Basically you don’t see a large bulk of images in your project navigator and searching any image folder wise one by one.
One more thing to notice, right click on image set/Asset Catalog and click ‘Show in finder’. In the project navigator you will find a asset folder corresponding to each images having 1x, 2x and contents.json file, describing each image. That file is automatically created by Xcode.

Types of Assets
There are three types of assets that can be included in an iOS asset catalog:
App icons - App icon sets contain the icons that are displayed on the iOS Home screen, Spotlight search results, the Settings App, and in the App Store.
Launch images - Launch image sets contain all the versions of the launch image displayed at startup for all of the targeted iOS devices.
Image sets - Image sets contain all the versions of an image that are necessary to support all of the targeted iOS devices. This includes images used in table views, tab bars, image views, and so on.

Why to use it?

There are some advantages of using asset catalogs:
1.)         One of the primary reasons for using asset catalogs is to make it easier to manage your app's images.
Suppose if your app supports both retina and non-retina devices. You need two version of your graphics 1x and 2x and need to maintain naming convention like, for icon it would be icon.png and icon@2x.png. With asset catalog you no longer need to maintain this convention.
2.)         A second advantage of using asset catalogs is performance. If your project has an iOS 7 deployment target, Xcode compiles your asset catalogs into a runtime binary file format that improves the speed of your app.
3.)          It also alleviates the need to keep the naming in sync since they share one name.

How to create and access it?

App Icons and Launch Image: Whenever you create a new project with iOS 7 as deployment target, you will get a folder in your project navigator named ''. This is where your App icons and launch images resides. Tap on it you will get something like this:



Now go to targets of App, At the bottom of the Project Editor, you should see the App Icons and Launch Images section.


 
Under App Icons, there are three sets of images—App, Spotlight, and Settings that indicate where each set of icons is used. The Kind column specifies the device and operating system for which a particular icon is used. The Dimensions column specifies the required size of the icon. The Resource column is intended to show the image included in your project that fits the criteria in the Dimensions column. The No image with correct dimensions found warning is incorrect, since our sample project does contain several of the required icons.
In the Launch Images section, there is a list of launch images for the iPhone.

To add an asset catalog for app icon and launch images, tap on  ‘Use Asset Catalog button’ at the top. It will display a pop up like this:



Tap on migrate, now your app will use images present in your ‘Images.xassets’ folder. Your view will get now updated to




Tap on Arrow on side of AppIcon/LaunchImage, you will get directed to asset catalogs folder.


Creating Asset Catalogs: For appicon and launch images we have built in asset folder, but what for other images that we are using in our app. Adding this images files to your create a bulk of images in project navigator, in that case if you want to search for a particular folder you have to open each and every folder until you are so sure where the image exactly resides.

Creating Asset catalog escape us from doing this:

Steps:
1.) Choose File > New > File.
2.) Under iOS or OS X, select Resource.
3.) Select Asset Catalog, and click Next.


4.)         Name the asset catalog, specify its location, and select the targets.



5.)         Click Create.

Your asset catalogs gets created. This newly asset catalog is empty for now.

Now it’s your choice, do you want to maintain images folder wise?
Or, want to add all images without maintaining folder structure?
Or, want to create separate asset catalog?

Here I will tell you how to maintain images folder wise?
Steps:
1.)         Tap on plus(+) sign at the bottom left corner or right click



2.)         Select ‘New Folder’
3.)         Your new folder gets created. Now Select that folder and right on it


4.)         Tap on ‘New Image Set
5.)         Double tap on the name of folder and image set to rename it. (Renaming image set is important as we will be accessing our images by this name only)


6.)         If your images not already added to your project just drag and drop your images to the block 1x and 2x,your images get added to your project. Alternatively, if you already added those images in your project, you need to import those files.





Accessing Asset Catalog: Accessing catalogs is same as you are accessing your images with ‘ImageNamed’. To load an image from an asset catalog, you can call the UIImage:imageNamed: method, passing the name of the set that contains the image.

For example, if your images set is named as ‘Search, you will access this search images using [UIImage imageNamed:“Search”]


Customizing Sets

Select a image set you want to customize and do customization like you can change name of your image set or make your images device specific.



Removing catalogs, or imagesets, or Folders
To remove a catalog, right click on that catalog and select ‘Delete’ option.
For imagesets or Folders, right click or control click on that folders, you will get option ‘Remove Selected Items’.



Disadvantages: Some few disadvantages when we implement asset catalog to manage our images are:
1.) You can no longer Command-Shift-O to look for an image.
2.) Another little annoyance is that Test Flight still wants to see an Icon.png file in your uploaded apps. The only way around it is to include a separate icon image in Xcode and add an icon entry back into the plist.

But as these advantages are not bigger than your App performance, I would recommend to use asset catalogs.

Happy Coding!!