Swift playgrounds with UIImage

2019-01-13 05:43发布

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?

11条回答
The star\"
2楼-- · 2019-01-13 05:57

For Xcode 9:

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

enter image description here

查看更多
爷的心禁止访问
3楼-- · 2019-01-13 05:59

As of Xcode 8 Beta 1, you can use Image Literals to import an image into an Xcode playground:

Start typing image to add an image literal:

Swift Image Literal

Select (or browse for) your image:

Image

See your image inline:

Inline

查看更多
冷血范
4楼-- · 2019-01-13 06:03

You can find out the path of resourcePath using these commands in playground:

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

Default for me was:

/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents
查看更多
疯言疯语
5楼-- · 2019-01-13 06:04

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

  1. Find your .playground file in finder
  2. Right click it -> show package contents
  3. If it doesn't already exist, create a folder named Resources inside the package
  4. Add your image there

Then just instantiate with let i = UIImage(named: "filename.png")

查看更多
时光不老,我们不散
6楼-- · 2019-01-13 06:05

I had some trouble with this also.

Unfortunately, Chris' answer didn't work for me. I suspect perhaps a later beta release of Xcode 6 may have removed this setting.

Here's a solution as of Xcode 6.0 beta 4 (6A267N) available 21st July 2014. I would guess that this corresponds to the "Inside playground" option previously. That is where the Resources folder is within the playground package itself.

Here's how to set this up.

Using Finder - or if you're like me and use the awesome Path Finder - right select and choose Show Package Contents as follows:

enter image description here

That reveals the packages Resources folder:

enter image description here

Copying the image files into that folder will do the business:

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

enter image description here

查看更多
地球回转人心会变
7楼-- · 2019-01-13 06:05

I had difficulty getting this setup for an iOS playground, as opposed to an OS X playground. Trying to do it using bundles and relative paths makes it more complicated.

If you just want to get your hands on an image quickly, you can also just use absolute file path:

On 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)
查看更多
登录 后发表回答