5

I spent two hours trying to make this simple code to work, with no luck.

What I did:

  • I put a resource in the Resources directory.
  • I referenced the resource as UImage(named: "resource.jpg")

This is not a duplicate question as the Playground settings went through a major change in 6.3.1

However, nothing is showing up. Here's a snapshot of my screen. As you can see there's no playground indicator running.

enter image description here

Mickey Mouse
  • 1,731
  • 3
  • 24
  • 40

2 Answers2

1

The biggest problem I see from your screenshot is that you don't even have the playground timeline open to see the results in the first place. The playground timeline is 1 column to the right of the playground gutter.

To view the playground timeline you use the Assistant Editor from the upper right. It is the button with the icon depicting 2 interconnected rings, or from the menu bar View > Assistant Editor > Show Assistant Editor.

assistant editor button

The second thing I figured out is that you have to put the UIImageView inside a regular UIView for the call to XCPShowView to work properly. See code below.

import UIKit
import XCPlayground

let image = UIImage(named: "pd.jpg")
let imageView = UIImageView(image: image)
XCPShowView("view", imageView) // Console error.

let bounds = imageView.bounds
let view = UIView(frame: bounds)
view.backgroundColor = UIColor.lightGrayColor()
view.addSubview(imageView)
XCPShowView("view", view) // This works!

Screen shot of the whole thing: playground screen shot

Jeff
  • 3,829
  • 1
  • 31
  • 49
0

For displaying an image in Playground you have to import XCPlayground.

import UIKit
import XCPlayground
Areal-17
  • 406
  • 3
  • 10