Emoji in string in UIImage(named: string) does not work

I have encountered this "bug" ? in compiling with Xcode 11.3.1.

The program compiles and runs fine but the code below does not execute, and the image does not load:

  • if let faceCardImage = UIImage(named: "J♥") {
  • faceCardImage.draw(in: bounds.zoom(by: SizeRatio.faceCardImageSizeToBoundsSize))
  • }

However if I change the code, only the string in the call to UIImage to a string without Emoji the image loads nicely

if let faceCardImage = UIImage(named: "11J") {

faceCardImage.draw(in: bounds.zoom(by: SizeRatio.faceCardImageSizeToBoundsSize))

}

Both images J♥ and11Jexist in Assets.xcassets.

For further information the code comes from Lecture 6 of the Stanford CS195 2017-2018 course.


I have tried different options of typing the string without any success.

I had it running ok in 2018, but is going through the course to refresh my memory.


Has anyone an idea of why this does not work ? Or has something changed in Swift since?

Any light/explanation on this problem will be highly appreciated.


Best regards

Lars C

Accepted Reply

To get past this problem, I manually recreated the imagesets for the assets. (previously I had copied them from GitHub).

This solved the issue. There must have been something wrong with the emoji used in the naming or in the imagefile.

Replies

the code below does not execute, and the image does not load:


if let faceCardImage =  UIImage(named: "J♥") {
            faceCardImage.draw(in: bounds.zoom(by: SizeRatio.faceCardImageSizeToBoundsSize))
        }


Do you mean image is nil ?


I tested in an app:


loaded an image named "Pho😀to" in resources


Called in viewDidLoad:


let faceCardImage = UIImage(named: "Pho😀to")

print(faceCardImage)


gets:

Optional(<UIImage:0x600000db4f30 named(Pho😀to) {24, 19}>)


I tested by moving to xcassets and removing from resources list.

Same correct result.


So problem is not emoji. Probably you have not copied the name correctly.


To exactly mimic your code, I changed the name as "Photo😀".

Works OK as well


let faceCardImage = UIImage(named: "Photo😀")

print(faceCardImage)


Optional(<UIImage:0x6000027e53b0 named(Photo😀) {24, 19}>)


Where do you call this code ?

When I do the call with a string containing only lettere, like yours UIImage(named: "Photos") it works.

Thats the equivalent of my working example UIImage(named: "11j".


It is when the call is made with a string that contains un emoji. UIImage(named: "J♥") that it does not work.

The result of the if let statement returns nil.


The name of the image in the Asset folder is "J♥".

So it is not a matter of a name that is not copied correctly.

Sorry, Claude 31, your post did not show up correctly, so I answered it wrong.


I tried a simple if let in a small program where it worked , but when pasting the code into my app it did not work.

So I'll go back to the drawing board and find the cause elsewhere in the program.

Thaks for your help

To get past this problem, I manually recreated the imagesets for the assets. (previously I had copied them from GitHub).

This solved the issue. There must have been something wrong with the emoji used in the naming or in the imagefile.