How to use Image Literal in Xcode 13
Asked Answered
B

6

28

The most used feature is missing in Xcode 13

Image Literal

#imageLiteral()

these commands do not seem to be working. A similar change has happened for Color Literals as well.

Bilharziasis answered 8/10, 2021 at 17:35 Comment(5)
Did you check this https://mcmap.net/q/503343/-quot-image-literal-quot-commands-not-working-in-xcode/1187415 ?Bustamante
@MartinR That's not consistently working for meBilharziasis
I'd give up if I were you. Image literals and color literals have not worked usefully in years. Xcode 13 is merely the latest iteration. If, as your question implies, you use them a lot, stop doing so.Example
In Xcode 13, use #imageLiteral( without the closing parenthesis. That will put the literal in the code and is the best workaround thus far. Code completion no longer works for literals in Xcode 13.Kaduna
@Rajesh, add images to the asset catalog first. The image literal requires images to be in an asset catalog before it will open.Kaduna
S
31

There is no longer image literal :(

You may try:

Who.What = UIImage(named: "catImage")
Stallard answered 7/11, 2021 at 10:40 Comment(2)
This is simpler than #imageLiteral( because the popup picker sometimes does not worksAutumnautumnal
Xcode 14.2 allows one to use an image literal simply as typing imageView.image = #imageLiteral() at which point the #imageLiteral() in the above changed to a clickable icon allowing you to select any image you have placed into the projects assets.Gamble
E
17

I came up with a workaround

Create code snippets for color literal and image literal like below

Color

#colorLiteral()

colorLiteral

And add completion shortcut as you want, Example: colorLiteral. Do same for Image literal

Image

#imageLiteral()

imageLiteral

And add completion shortcut as you want. Call those completion shortcuts in your code to get color and image literals.

colorLiteral imageLiteral

Endamage answered 29/11, 2021 at 7:56 Comment(0)
R
11

type Who.What = #imageLiteral( Currently it works like this.

Ronni answered 7/11, 2021 at 15:27 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Exertion
N
7

I used

UIImage(imageLiteralResourceName: "image_asset_name")

and it works perfectly fine on XCode 13.2.1

Necessity answered 22/2, 2022 at 14:24 Comment(1)
Confirmed in MontereyGilli
S
0

You need to set a variable at the top of your struct to hold the color and image literals before you use them in the view body.

let color = #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)
let image = #imageLiteral(resourceName: "cherry1")

creating a shortcut is handy but it doesn't solve the problem of Xcode not allowing literals to be used as parameters to views.

This is a nice feature in Xcode and you may find it cool for small projects, but the best way to handle images, colours and other resources is to look into a package like R.Swift or Swiftgen

https://github.com/SwiftGen/SwiftGen

https://github.com/mac-cain13/R.swift

I prefer swiftgen since R.swift is not currently working with M1 Macs.

Scenarist answered 9/3, 2023 at 15:17 Comment(0)
B
0

I can't make Xcode auto complete colorLiteral, but you can still use the inline color picker.

This is a workaround which works in Xcode 14.2.

Where you want the color picker, enter any color full colorLiteral

#colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)

Xcode will then display the color picker which you can use.enter image description here

Bookerbookie answered 18/3, 2023 at 17:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.