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)
Related
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:
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.
One of our team member is urging to use SVGs using github.com/pocketsvg/PocketSVG, instead of regular assets #1x #2x #3x. We have over a hundred image sets.
I have 2 questions:
1) 100s of SVG data rendered using PocketSVG or even another library, is that gonna be a performance kill?
2) Using SVGs over regular assets, going to make any different visually, though our designer have these all images extracted properly using vectors.
Thanks in advance.
Answer for first Question : Yes, Whenever trying to show an image on the screen, it will process your SVG file and create a new image assets. May be the library can cache the image to avoid second time processing.
You no need to use PocketSVG if you have all SVG images on your Asset Catalog. Because Xcode Asset catalog itself able to handle the SVG images.
Xcode will create #1x #2x #3x images from SVG file at the time of compilation.
Answer for second Question : SVG is a vector image. You can extract good quality image from svg. You will not get any different by using #1x #2x #3x images or SVG image.
The advantage of using SVG is no need to create one more asset #4x if Apple introduced another different screen resolution devices. Just recompile the code Xcode will create on behalf of you
Refer the link : How to use vector in Xcode
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.
With the need of 3x images in iOS 8 I figured it would be easier to manage images with an asset catalog in Xcode 6. I am trying to just provide a single image, the 3x scaled image to the asset catalog and I want Xcode to automatically downscale that image to generate 2x and 1x versions of the 3x image, so that I don't have to do it manually.
Right now this is what I have:
I really hope that Xcode has such a feature to automatically create 2x and 1x images and I just have not discovered it yet. If there isn't any such feature, are there any alternatives? (I know IconKit but it is annoying and hasn't been updated to support 3x images. If it was made properly in the first place it wouldn't even need an update..)
I know that Xcode automatically can generate all images from a vector pdf, but thats not what I am looking for!
I ended up making a small mac app that takes care of the dirty work. You can drag and drop one or multiple .jpg or .png files into the app's main view; it will detect the input image's scale and create the smaller scaled images. With this app I only need to create a single, 3x image and drag it into the app to create the two smaller images. It saves so much work!
The source can be found on my GitHub page, simply download it and run it on your mac! https://github.com/JonasGessner/ImageReducer
There is another way to solve this problem.
Use Automator to create Mac OS X service which create #3x, #2x and normal image on base of 1 image in #3x resolution automatically. It use just 3 methods: duplicate images, scale down, and rename.
Ready Mac OS X service made in Automator you can find here:
https://github.com/lukszar/iOS-Images-Prepare
late to the party but there is one tool which make all assets #1x, #2x, #3x and icons for you in just one click. And also export to XCAssets file.
You can find it here: https://github.com/angelvasa/AVXCAssets-Generator
Hope this will help someone
If you do not provide 1x or 2x assets, and your app is run on a 1x or 2x device, it should downsample the 3x asset at runtime. This may look fine, but depending on the scaling method used, it may also look really bad, so make sure this is really what you want.
If a folder named *.imageset, and the contents in it is well organized, Xcode will actually recognize them as an image. So this is another way to do this: https://github.com/albert-zhang/gen_xcassets
This python script will quickly auto generate #2x version for all images in a folder.