How to take screen shot programmatically (Swift, SpriteKit)
I think this question could be merged with this one
Screenshotting on Iphone in swift only has a white background
which seems the same
EDIT:
I think I found a solution.
First read this post: How Do I Take a Screen Shot of a UIView?
I create an Extensions.swift file in which I 'extended' the methods of a UIView using that link.
After that I simply connect the following to my sprite-kit button
let screenshot = self.view?.pb_takeSnapshot()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
Voilà, the image is in the camera roll!
Taking and sharing Screenshot using Swift in SpriteKit
You didn't show how you are using shareScore()
function so I have created one simple example for you from the tutorial you given and it's working fine and below is complete sample code:
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
override func didMoveToView(view: SKView) {
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
self.addChild(sprite)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if sprite.containsPoint(location) {
let postText: String = "Check out my score! Can you beat it?"
let postImage: UIImage = getScreenshot(scene!)
let activityItems = [postText, postImage]
let activityController = UIActivityViewController(
activityItems: activityItems,
applicationActivities: nil
)
let controller: UIViewController = scene!.view!.window!.rootViewController!
controller.presentViewController(
activityController,
animated: true,
completion: nil
)
}
}
}
func getScreenshot(scene: SKScene) -> UIImage {
let snapshotView = scene.view!.snapshotViewAfterScreenUpdates(true)
let bounds = UIScreen.mainScreen().bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
snapshotView.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshotImage;
}
}
And when you click on Spaceship image you will get result like:
Now you can easily share it.
Sample code for more Info.
How Do I Take a Screen Shot of a UIView?
I think you may want renderInContext
, not drawInContext
. drawInContext is more a method you would override...
Note that it may not work in all views, specifically a year or so ago when I tried to use this with the live camera view it did not work.
Screenshot showing up blank - Swift
update: Xcode 8.2.1 • Swift 3.0.2
You need to add the import statement and this extension to your game scene:
import UIKit
extension UIView {
var snapshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
defer { UIGraphicsEndImageContext() }
drawHierarchy(in: bounds, afterScreenUpdates: true)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
let myImage = view?.snapshot
Nothing loading to screen in SpriteKit when done programmatically
No where in your code are you specifying that you want to open GameScene.
// Create a scene
let gameScene = SKScene(size: skView.bounds.size)
if you do not have a corresponding SKS file created in the scene editor but would rather load the scene that was created in code use...
what it should be...
// Create a scene
let gameScene = GameScene(size: skView.bounds.size)
Related Topics
Nsnumberformatter:Show 'K' Instead of ',000' in Large Numbers
Self.Image.Frame.Width = 20 Give Get Only Property Error
How to Override Layout of Nstableheaderview
How to Hide the Navigationbar When Embedding Swiftui in Uikit
Convert Bar Chart to a Grouped Bar Chart with Danielgindi/Ios-Charts and Swift
Swiftui Navigation on iPad - How to Show Master List
How to Link Xctest Dependency to Production/Main Target
Binary Operator '+' Cannot Be Applied to Two 'T' Operands
Asyncdetached Falling Back into Main Thread After Mainactor Call
Swift: Extract Float from Byte Data
Lazy Readonly Property in Swift
Swift: Loop Over Array Elements and Access Previous and Next Elements
Undefined Behavior, Or: Does Swift Have Sequence Points
How Does Typecasting/Polymorphism Work with This Nested, Closure Type in Swift