Swift version: 5.6
iOS has a simple, beautiful solution for handling Retina and Retina HD graphics, and in fact it does almost all the work for you – all you have to do is name your assets correctly.
Imagine you have an image called taylor.png, which is 100x100 pixels in size. That will look great on non-Retina devices, which means iPad 2 and the first-generation iPad Mini. If you want it to look great on Retina devices (which means iPad 3, 4, Air, Air 2, Mini 2, Mini 3, Pro, plus iPhone 4s, 5, 5s, 6, and 6s) you need to provide a second image called firstname.lastname@example.org that is 200x200 pixels in size – i.e., exactly twice the width and height.
Retina HD devices – that’s the iPhone 6 Plus, iPhone 7 Plus, and iPhone 8 Plus – have an even higher resolution, so if you want your image to look great there you should provide a third image called email@example.com that is 300x300 pixels in size – i.e., exactly three times the width and height of the original.
If you're not using an asset catalog, you can just drag these images into your project to have iOS use them. If you are using an asset catalog, drag them into your asset catalog and you should see Xcode correctly assign them to 1x, 2x and 3x boxes for the image. It's critical you name the files correctly because that's what iOS uses to load the correct resolution.
With that done, you just need to load taylor.png in your app, and iOS will automatically load the correct version of it depending on the user's device.
SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!
Available from iOS 8.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.