Search code examples
iosuiimageswiftxcode6swift-playground

Swift playgrounds with UIImage


I am working with Xcode 6, and I'm trying to recreate the code demoed during session 401 "What's new in Xcode 6". I've added an image to Images.xcassets (called Sample) and within the playground file I'm trying to access this image, as demoed.

My code is as follows (like the demo):

var sample = UIImage(named: "Sample")

However, I can't get it to work like the demo. Am I missing something?


Solution

    1. Open the .playground file in Finder.
    2. Create a folder called Resources next to it.
    3. Add any images you want to this folder.
    4. In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.

    File Inspector

    1. Under 'Resource Path' choose 'Relative To Playground'
    2. Click the folder icon underneath and choose the Resources folder created earlier.

    You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):

    Working nsbundle image

    Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.