Peter Parker Peter Parker - 3 years ago 103
iOS Question

Assets cannot be found

Before xCode 9, when I wanted to create a UIImageView with an image, I would drag an image into the project side bar and create a UIImage with the name of that file. This worked and all was well.

However, with Xcode 9 this will not work. It won't be able to find that file, even though it's there in the main bundle. What I needed to do was to put the image into the Assets folder, and then it was able to find the image and show it in the UIImageView.

Likewise, when working with Sprite Kit, I would create a folder in my desktop with a .atlas extension, drag all my images into that, and then drag the .atlas folder in the project. Then I'd create a SKTextureAtlas with the name of the .atlas folder, and it worked. With Xcode 9 however, this again did not work. I would get a 'Texture Atlas 'foo' cannot be found' message in the console. So I went to the Assets folder, clicked the add icon and choose New Sprite Atlas, renamed that folder, and dragged my images into it. Now creating the texture atlas with that name worked.

What I'm now trying to do, and having trouble with, is creating an SKVideoNode using init(filedNamed: "foo.mov"), and as you guessed, this no longer works if you dragged the file into the project's side bar. So I thought maybe I again need to somehow do this through the Assets folder. I found that when I add my .mov file to the Assets folder, it gets created as a 'Data' type ('Data' shows in the top right corner of its box in the Assets folder). What I can do now is this:

let d = NSDataAsset(name: "foo") // 'foo' is the .mov file in Assets
d.data // the underlying content


Turns out d is not nil, which was good news, but the only way to access the underlying content is just by way of .data, and you cannot create an SKVideoNode with data. I also wanted to load a sound file, which I used to do like this:

let a = SKAction.playSoundFileNamed("bar.mp3", waitForCompletion: false)


But I get the message 'Error loading sound resource: "bar.mp3"'.

Answer Source

Check to see if your resources are being bundled.

Go into your project settings and click on the Build Phases tab, make sure your resources are in the copy bundle resources section enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download