3

I've spent hours trying to simply gain access to image data in my Swift Playground. I've followed many tutorials that recommend adding a resource folder as a sibling dir in the dir. your playground is in. Or to drag images into the Resources folder in the playground Navigator. Nothing has worked. What do I have to do to load an image view in playground?

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80
  • These methods work. Show us why they don't work for you: screenshots of your playgrounds may help finding the issue, for example. Also, open the playground's debug area and tell us what are the error messages. – Eric Aya Aug 11 '17 at 15:40
  • there are no errors. the sources and resources folders are simply greyed out. dragging images into them has no effect. I have employed the methods I described before and has success. I'm not sure why it isn't working now. I am using Xcode8 and Xcode9 on the same machine and that often causes problems with simulators. I wonder if switching back and forth is somehow corrupting playgrounds in some way. – Alex Bollbach Aug 11 '17 at 16:30
  • Switching between Xcode versions may cause issues - I know it happened to [me in the past](https://stackoverflow.com/q/30752203/2227743). // How about image literals? You just drag the image from the Finder to the playground *into the code editor, next to your variable - not in the Resources folder*, the playground will import the file itself. Does it work? Like you do `let img =` and you drop the image from Finder just next to the `=`. – Eric Aya Aug 11 '17 at 16:33
  • THAT was actually working but when i tried `let img = THAT` i got errors – Alex Bollbach Aug 11 '17 at 16:48
  • now it seems that `let a = #imageLiteral(resourceName: "big.png")` is working. at least this was not working.. `let a = #imageLiteral(resourceName: "big.png") let im = UIImage(imageLiteralResourceName: a)` – Alex Bollbach Aug 11 '17 at 16:50

1 Answers1

10

I solved this problem on my side when I

  1. import PlaygroundSupport
  2. mark Resources Folder and tap + icon
  3. Choose Add files to 'Resources' and choose your image (here: image.jpg)
  4. Instance your image via let image = UIImage(named: "image.jpg")

Xcode 12 Preview

enter image description here

SchmidtFx
  • 498
  • 4
  • 16
  • 1
    FYI: Using Xcode 9.2.3, the display of the image (inline in the grey rounded-rect) required tapping on the "Show Result" (looks a bit like an iconized computer CRT) on the far right side. – benc Oct 26 '18 at 16:39
  • This worked for me in Xcode 12.5.1, too. Adding "import PlaygroundSupport" was a step not included in a much older SO post: https://stackoverflow.com/questions/24069479/swift-playgrounds-with-uiimage – Rethunk Sep 13 '21 at 17:34