I just started using Asset Catalogs in Xcode, so had few questions about better organization of catalog.
I have over 100 button images, each image has 5 versions(#1x, #2x, #3x, #1x iPad, #2x iPad).
One way is to create 100 image sets in the catalog file. However, that would be too clumsy. Is it possible to group related image sets together?
Is there a better way of importing 500 images than manual drag and drop?
Name your image files using the #x~device format, such as:
btn1#1x~iphone.png
btn1#2x~iphone.png
btn1#3x~iphone.png
btn1#1x~ipad.png
btn1#2x~ipad.png
You can then add them as individual files in your project, putting them in groups / folders as desired:
Now, you can set a button (or image view, etc) image via code:
if let img = UIImage(named: "btn1") {
button1.image = img
}
and iOS / UIKit will automatically use the correct image for the device.
Or, you can set the image in Storyboard / IB. Note that when you select the image from the drop-down list in Attributes Inspector pane, it will show as:
btn1.png
however, at run-time -- or, if you use Preview or View as: to change the device in Xcode -- the correct image will be used.
Give it a try with these images:
The #1x images are 128 x 128, the #2x images are 256 x 256 and the #3x image is 384 x 384.
Note: You can also use Asset Catalog without having to drag the files one-by-one. If you have the set of 5 files names as above, open an Asset Catalog in Xcode and drag-and-drop all 5 files into the Catalog. Xcode will automatically configure them:
Related
If I have #3x image and all I need to do is downscale that image to create #2x and #1x, why am I required to add them? I see some UIImage functions just downscale #3x image on the device whereas other UIImage functions fail if #2x image is not there. I find it very time consuming to downscale so many images and add them to project every time. Is there a workaround?
Edit: To make it more clear, my question is UIImage imageWithContentsOfFile returns nil if I only supply #3x without supplying #2x. But UIImage imageNamed works. Is that normal, or I am doing something wrong?
Not sure exactly what you need to do or how you are using the scaled images... or even if they are photos or icons.
If you are using these images as icons, add them to an asset folder. Then the assets can be referenced by name within a control on a storyboard. Easy.
For creating buttons, let’s say a 40x40 button, I create the icon, 40 x 40, in Inkpad and save the result as a PDF file. Then I put that in an image asset and select the option for only one image. Since it is a vector drawing, it scales up nice.
If you are using the same images in multiple apps, I would recommend creating an assets folder called something like Common_Images and just copy that folder from one app to another. Deliriously quick & easy,
I wrote a quick and simple app that lets the user pick a photo, type in the 1x width and height, press a button and done! Doing this on a Simulator lets me quickly drag and drop these new scaled pics right into an image asset within Xcode. Quick and simple.
I have a script that I wrote that takes a folder of images, and automatically resizes them to the #3x, #2x, and #1x equivalents.
I named the folders #3x, #2x, #1x, and inside each folder every image has a suffix of #3x.png, #2x.png, #1x.png, respectively. (So for example bouncing-ball-frame-1#3x.png resides in the #3x folder)
The way I used to import images was just this:
Manually create a new sprite atlas folder in XCAssets using the "+" button in Xcode
Manually drag and drop the #3x images into this folder. Xcode will then use these images as their #3x versions.
Manually (1 by 1) move each #2x image, and each #1x image, into their respective "slots" next to the #3x image.
But now that I have tons of images i'll be using, this is just a lot of work for me.
I was hoping I could just drag and drop an entire folder into XCAssets (with all of the 3x,2x,1x images), and Xcode would automatically put each image where it belongs in one giant sprite atlas.
Is this sort of thing possible? I could've sworn I read somewhere that if you name the folders/images a certain naming convention, and suffix the parent as .atlas, then it does this all for you without having to manually do it.
Could anyone offer advice/solution for this?
Thanks!
I figured out how to do this, and wanted to share:
You create a new atlas within Xcode, and remove the default image it gives you.
..Now, create a folder on your computer, and put all of the images in there. EVERY image. The 1x, the 2x, and the 3x.
The naming conventions should be like this (assume my images are 25 frames like bouncing-ball-1.png, bouncing-ball-2.png, etc)
bouncing-ball-1.png would be the 1x version (notice the absence of 1x?)
bouncing-ball-1#2x.png
bouncing-ball-1#3x.png
If you had 25 frames of animation, you'd have 75 images in one folder.
Then you drag all 25 of these files to your target xcassets/ folder I mentioned above, and you're done.
Xcode will look at bouncing-ball-1, bouncing-ball-1#2x, bouncing-ball-1#3x, ..., etc, and create 1 image for all 3 sizes (so in my case 25)
I am trying to use .pdf image in xcode asset catalog. So all #1x, #2x, #3x images will be generated by the xcode in build time.
The problem I am facing is i am not able to find the type option in the inspector in xcode 7.3. Only scale factor is available instead of types If i use scale factor image is not retrieved
I love using the new PDF vector images in Xcode 6 Asset Catalogs a.k.a xcassets. I know that on iOS, these PDF's are rendered down at build time to #1x, #2x etc. PNG's and then embedded into the runtime asset catalog.
However I need at least two different sets of sizes: standard and double-sized. Effectively, I would need #1x, #2x and #3x for the standard icons and #2x, #4x and #6x for the double-sized icons.
Question: how can I get these non-standard sizes from the single PDF? I don't necessarily need a fully dynamic sizing, just two sets of sizes. If Xcode 6 doesn't support this, are there any easy tools that will simply double the size of the PDF?
I'm creating custom icons for the WatchKit Force Menu. The documentation says to use an 80 x 80 size image with a drawing area of 54px square. All that works fine, but my image, when displayed in the button, looks very blurry compared to the built-in button images.
I'm creating them in Illustrator at 80px square. Saving as a .png image like the documentation says. Sizing is correct when saved at 72 dpi. If I do anything higher it causes the image in the button to be too large. I cannot find a way to scale the image.
Has anyone run into this? It seems like I would want to use a higher resolution image here or vector graphics.
You need to save the file with #2x in the filename to support retina displays.
So if your filename is myicon.png rename it to myicon#2x.png. In code you just use myicon for the name, Xcode automatically picks the correct size.
For iPhone 6, #3x is required...
I would recommend to use the Images.xcassets in Xcode for maintaining all images. There are templates for all needed resolutions (#1x, #2x, #3x, ...). Create the icons in these several resolutions and drag the files from finder to the placeholders. Later in your code you simply use the name of the image set in Xcassets.