Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
Ross Gibson Avatar asked Jun 05 '14 20:06

Ross Gibson


People also ask

Is Swift Playgrounds a good way to learn to code?

Learning to code with Swift Playgrounds is incredibly engaging. The app comes with a complete set of Apple-designed lessons. Play your way through the basics in “Get Started with Code” using real code to guide a character through a 3D world. Then move on to more advanced concepts.

Is Swift Playgrounds good for adults?

Simply put, after about an hour, even grown adults should be fully engaged by Swift Playgrounds' playful coding puzzles. That's a big win. Parents and children can go through these activities together, and they can both really get something out of it.


2 Answers

Look at the iOS Developer Library->Playground Help and search"Resource Files" and you will find the answer

1、Open the .playground

2、Show the Project navigator by selecting View > Navigators > Show Project Navigator.

3、Drag the images to the Resources

Like follow:

enter image description here

like image 58
Bruce Avatar answered Oct 02 '22 18:10

Bruce


  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.

like image 37
Chris Hill Avatar answered Oct 02 '22 17:10

Chris Hill