Swift playgrounds with UIImage
- Open the .playground file in Finder.
- Create a folder called Resources next to it.
- Add any images you want to this folder.
- 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.
- Under 'Resource Path' choose 'Relative To Playground'
- 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"):
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.
How to use Image() in Swift Playgrounds for IPad
The following is what I did and worked for the iPad:
import SwiftUI
extension Image {
// usage: Image(name: "imageNameHere.jpg")
public init(name:String) {
self.init(uiImage: #imageLiteral(resourceName: name))
}
}
or
extension Image {
// usage: Image(name: "imageNameHere.jpg")
public init(name: String) {
self.init(uiImage: UIImage(named: name)!)
}
}
PS: Don't forget to import your image into Swift Playgrounds for iPad first.
Swift Playground add a WORKING image view
You drag it into the Resources folder, then access it from the Bundle. Sample code:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 916, height: 611))
view.backgroundColor = .white
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 916, height: 611))
view.addSubview(imageView)
if let sample = Bundle.main.path(forResource: "sample", ofType: "jpg") {
let image = UIImage(contentsOfFile: sample)
imageView.image = image
}
PlaygroundPage.current.liveView = view
PlaygroundPage.current.liveView
Here's the result:
Related Topics
Jenkins - Xcode Build Works Codesign Fails
"Do Not Embed", "Embed & Sign", "Embed Without Signing". What Are They. What They Do
How to Send Data Back by Popviewcontrolleranimated for Swift
Xcode 8 :Function Types Cannot Have Argument Label Breaking My Build
Ios9 - This Application Is Modifying the Autolayout Engine from a Background Thread -- Where
How to Change My iOS Applications' Entitlements
Alertcontroller Is Not in the Window Hierarchy
Receiving Push Notifications While in Background
How to Get Download Progress in Afnetworking 2.0
How Does Apple Notify iOS Apps of Refunds of In-App Purchases (Iap)
How to Get Rgb Values from Uicolor
Current Week Start and End Date
How to Know Push Notification Delivery Status
What Is the Second Parameter of Nslocalizedstring()