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.
ios Sprite Kit screengrab?
You almost have it, but the problem is as explained in the comment above. If you want to capture SKScene
contents, try something like this instead:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self.view drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The solution is to basically use the new method drawViewHierarchyInRect:afterScreenUpdates
instead, which is the best we have for now; note, it's not exactly speedy, so doing this in realtime will not be pretty.
How to grab a screenshot in sprite kit?
I’m not sure whether -drawViewHierarchyInRect:afterScreenUpdates:
is supposed to work with SKView (I assume so), but the reason that second example doesn’t compile is that (1) you’re trying to call it and bounds
on self
(presumably a UIViewController) rather than self.view
and (2) you haven’t defined the value of scale
. Just change those two lines:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
Also, the second parameter to UIGraphicsBeginImageContextWithOptions
—“opaque”—shouldn’t be NO unless your view is transparent, and setting “afterScreenUpdates” to YES is only necessary in some circumstances which probably don’t include this one. You may see better performance by changing those.
SpriteKit : Why can't I hide SKSprite nodes and take a screenshot right after that?
Most likely because you do both hiding and taking screenshots during the update of the same frame. So what you actually take a screenshot of is the screen contents of the currently displayed frame which doesn't show any changes like hidden buttons until Sprite Kit renders the screen anew at the end of the update/actions/physics cycle.
You will have to wait for one frame so Sprite Kit renders the scene with the buttons removed, then you can show the buttons again. Note that this will inevitable cause the buttons to flicker.
To wait for one frame, it should suffice to run a runBlock action:
[self runAction:[SKAction runBlock:^{
UIImage* screenshot = [self captureFullScreen];
}];
If it doesn't suffice, run the action in didEvaluateActions
or didSimulatePhysics
to ensure the action is scheduled to run the next frame.
How to make the scene stretch out fully to the width. Screenshot attached of my Xcode simulator of it not filling the screen
Once again, this happens because Apple has mistakenly removed the launch screen from the iOS-only SpriteKit game template (it's still there in the multi-platform template). You need to either add and assign a launch screen or choose Main as your launch screen.
The launch screen defines the working area for the app, so without it the view bounds and scene scale mode will not fix this issue.
Man, this is really going to continue being an issue that people are stumped by. Quite an oversight on Apple's part!
More info in Background is not filling the whole view SpriteKit.
Related Topics
Should I Git Ignore Xcodeproject/Project.Pbxproj File
How to Create an Ibinspectable of Type Enum
Ipad: Drag a Uitableviewcell from One Uitableviewcontroller and Drop It into Another
Ios5 What Does "Discarding Message for Event 0 Because of Too Many Unprocessed Messages" Mean
iOS 6 Rotations: Supportedinterfaceorientations Doesn't Work
How to Zoom a Uiscrollview Inside of a Uicollectionviewcell
How to Select Range of Values When Using Arc4Random()
Request Permissions Again After User Denies Location Services
Error While Build Project Xcode Says:"You Don't Have Permission"
How to Wake Up iOS App with Bluetooth Signal (Ble)
Uicollectionview Scrolling in Both Directions
Uialertcontroller Text Alignment
Install iOS App Directly from Website - No Password, No Itunes, No Udid
Swiftui Datepicker Jumps Between Short and Medium Date Formats When Changing the Date