How to Take Screen Shot Programmatically (Swift, Spritekit)

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:

Sample Image

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



Leave a reply



Submit