33

In Xcode 8, you have images autocompleted while typing.

Question is : Why?

I tried to init UIImage with the suggested result, but it doesn't work.

Inside UIImage Outside UIImage

Does anyone know how to use it ?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
CZ54
  • 5,488
  • 1
  • 24
  • 39
  • I think this only works in playgrounds it appears. At least that's what it seems like. – NickDK Sep 21 '16 at 20:30
  • 2
    You need to give some more information, maybe share the surrounding code and Xcode version. Image literals work fine for me, not only in playgrounds. – James P Sep 29 '16 at 09:53
  • This is the most aggravating feature I've encountered with Xcode. It's like, "Here's a timesaver...PSYCH!" – Adrian Oct 07 '17 at 02:47
  • 1
    I'm using Xcode 9 and Swift 4 and I don't see the image preview in "intellisense". – Van Du Tran Nov 01 '17 at 16:19

4 Answers4

47

Xcode 8 will automatically recognize any images you’ve got in an Asset Catalog and offer them up as a suggestion inside of a UIImage initializer.

So basically what you need to do is just the following (as you have done in your question, but there must be something else that´s disturbing it):

let i = voiture // image name

And then just use i when you want to set an image.

Under the hood it’s creating code that looks like this: #imageLiteral(resourceName: "voiture.png"). But inline in the source editor, you’ll just see the file name of the image. The #imageLiteral syntax is only recognised on Swift 3 or later.

Here is a demonstration video link where I do this and here is a link to a sample project that I created.

Note that you need to click on the instellisense suggestion so that you see a thumbnail of the image in the code and then the image name.

Update 1

This functionality remains in Xcode Version 9.0 beta 6 (9M214v) enter image description here

Update 2

Xcode 11.2.1, this is not appearing anymore.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • If you take a look a the second screenshot, I got a compiler issue and i'm not able to build... – CZ54 Sep 29 '16 at 09:24
  • 2
    As I wrote in the answer there must be something else disturbing it. Did you watch the video that I uploaded it´s the same way I have done it. [Here](https://drive.google.com/open?id=0B2TR9xoKqvZNeFNpNHBJOXFSbm8) is a link to the project I created, try to download and run it and see if it works for you. – Rashwan L Sep 29 '16 at 09:28
  • 2
    The only diff between your sample and my app, is that you are in Swift 3. So I guess this is it – CZ54 Oct 06 '16 at 08:05
  • Just an update. Image literal doesn't show the image and image name when you set the device to iPad in an image sets. If it's set to Universal, it works then. – aytek Feb 21 '17 at 12:24
10

An image literal already is a UIImage, you don't need to pass it to a UIImage to initialize one.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
  • Heheh - apparently, Xcode is smart enough to infer the type as a UIImage if the context makes it so, or else there would be a compile time error for the code, above. – Victor Engel Aug 15 '20 at 15:24
10

I'm unable to find an "official" source for it, but from my personal testing it appears that while the autocomplete works in both Swift 2.3 and Swift 3.0 projects, image literals will only work correctly in a Swift 3 project.

Give it a try in a Swift 3 playground and you'll find it works just fine.

enter image description here

Dave Lyon
  • 613
  • 5
  • 8
4

I have been using this feature. Basically if I have an image that begins with "ic", I would type let i = followed by ic. At this point, Xcode 8 will show a dropdown of images and I then choose the correct image from the list. I have never bothered to actually type UIImage(. It works just like autocompleting code except that it uses the filename of the image.

In your case, there is probably some other issue causing the compilation to fail.

Hong Wei
  • 1,397
  • 1
  • 11
  • 16