Fast Playgrounds with UIImage

I am working with Xcode 6 and I am trying to recreate the code demo during the 401 session “What's New in Xcode 6”. I added an image to Images.xcassets (called Sample), and in the playground file I am trying to access this image as shown.

My code is as follows (e.g. demo):

var sample = UIImage(named: "Sample") 

However, I cannot get it to work as a demo. Did I miss something?

+44
ios swift xcode6 uiimage swift-playground
Jun 05 '14 at 20:16
source share
11 answers
  • Open the .playground file in Finder.
  • Create a folder called Resources next to it.
  • Add any images you want to this folder.
  • On the playground, click opt-cmd-1 to open File Inspector. You should see the site on the right. If you did not select it, press cmd-1 to open the Project Navigator and click on the playground file.

File inspector

  1. In the "Path to resources" section, select "Regarding the playground"
  2. Click the folder icon at the bottom and select the Resources folder that you created earlier.

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

Working nsbundle image

Note. Since Xcode often overwrites the .playground folder, I recommend using this method, so the resource folder will not be permanently deleted and re-created.

+56
Jun 08 '14 at 18:33
source share
— -

Look at the iOS Developer Library-> Help on the Playground and search for “Resource Files” and you will find the answer

1, open .playground

2, show the project navigator by choosing View> Navigators> Show Project Navigator.

3, drag and drop images into Resources

Properly:

enter image description here

+69
Jun 11 '15 at 3:26
source share

I also had problems with this.

Unfortunately, Chris's answer did not work for me. I suspect that perhaps a later beta version of Xcode 6 could remove this setting.

A solution from Xcode 6.0 beta 4 (6A267N) is available here, available July 21, 2014. I would suggest that this matches the “Indoor Playground” option earlier. This is where the Resources folder is located inside the playground package.

Here's how to set it up.

Using the Finder - or if you are like me and using the awesome Path Finder - select the correct one and select Show package contents as follows:

enter image description here

This shows the Resources folder:

enter image description here

Copying image files to this folder will do business:

 let imageNames = ["back-button", "background", "bg_top"] let images = imageNames.map { UIImage(named: $0) } 

enter image description here

+32
Jul 31 '14 at 11:20
source share

However, I cannot get it to work as a demo. Did I miss something?

I'm not sure where you need to put the image to link to it using only the name, but I got the same code to work, indicating the full path to the image, for example:

 var sample = UIImage(named: "/Users/my_user_name/Desktop/Image1234.jpg") 

Using the full path seems more complicated than it should be, but it works, and it allows me to move on to more interesting issues.

+3
Jun 05 '14 at 20:35
source share

You can find out the resource path using these commands on the playground:

 var bundle = NSBundle.mainBundle() var path = bundle.resourcePath 

The default for me was:

 /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents 
+3
Jun 06 '14 at 2:31
source share

It was difficult for me to get this setting for the iOS playground, unlike the OS X playground. Trying to do this using bundles and relative paths makes it more difficult.

If you just want to quickly get the image, you can also use the absolute path to the file:

In iOS:

 # iOS let absoluteImagePath = "/absolute/path/to/image.png" let image = UIImage(contentsOfFile: absoluteImagePath) 

And on OS X

 # OS X let absoluteImagePath = "/absolute/path/to/image.png" let image = NSImage(contentsOfFile: absoluteImagePath) 
+3
Jun 17 '14 at 4:26
source share

As with Xcode 8 Beta 1, you can use Image Literals to import images on the Xcode playground:

Start typing image to add an image literal:

Fast image

Select (or view) the image:

Picture

See image in line:

Inline

+3
Jun 13 '16 at 22:08
source share

This is what worked for me on Xcode Version 6.1.1.

  • Create a playground file in the same directory as the main storyboard.

    Folder structure

  • Open the Utilities panel for the Playground file and click the right arrow in the Resource Path section to add images to this directory.

    Resource path for playground

  • Check the image in the playground file.

    Result

+2
Feb 10 '15 at 21:58
source share

On the iOS playground with Xcode 6 beta 5 (and possibly later) you can see the image inside the package:

  • On the playground, press Cmd + Opt + 1 and click the arrow under Resource Paths (this will open the Finder)
  • Put the image in this folder
  • Access it using

     let img = UIImage(named: "logo.png", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: nil) 

    or

     let path = NSBundle.mainBundle().pathForResource("logo", ofType: "png") let img = UIImage(contentsOfFile:path) 

    or in Swift 4:

     let path = Bundle.main.path(forResource:"logo", ofType: "png") let img = NSImage(contentsOfFile:path!) 
+1
Sep 09 '14 at 17:01
source share

Using Xcode 6.3.1 and running the playground in a full simulator (iOS), I had to do the following:

  • Find your .playground file in finder
  • Right click -> show package contents
  • If it does not already exist, create a folder named Resources inside the package
  • Add your image there

Then just create an instance of let i = UIImage(named: "filename.png")

+1
May 28 '15 at
source share

For Xcode 9:

  • Select Resources
  • Right-click " Add Files to Resources "
  • Use it like: let image = UIImage(named: "no")

enter image description here Resources

0
Dec 27 '17 at 5:41 on
source share



All Articles