拍摄和分享在SpriteKit中使用Swift的屏幕截图

问题描述:

我刚刚在SpriteKit中使用Swift进行了一个简单的游戏。在结束时,它显示Score,Share和Restart按钮。拍摄和分享在SpriteKit中使用Swift的屏幕截图

为最终场景的截图分享功能仍悬而未决。我在这里发现了一个非常简单的解决方案,我不是专家,因此我迄今为止所有尝试实施此解决方案的尝试都是徒劳的。

我试图调用shareScore()函数(如在该解决方案中写的),当玩家点击shareButton()但它不起作用。

这里是我的EndScene

var restartBtn = SKSpriteNode() 
var shareBtn = SKSpriteNode() 
var scoreLbl = SKLabelNode() 
var score = Int() 


override func didMoveToView(view: SKView) { 

    scene!.scaleMode = .AspectFill 

    self.view?.backgroundColor = UIColor.grayColor() 

    scoreLabel() 

    shareBtn = SKSpriteNode(imageNamed: "Share") 
    shareBtn.position = CGPointMake(self.frame.width/2, self.frame.height/3) 
    shareBtn.setScale(0.0005) 
    self.addChild(shareBtn) 

    restartBtn = SKSpriteNode(imageNamed: "Restart") 
    restartBtn.position = CGPointMake(self.frame.width/2, self.frame.height/4) 
    restartBtn.setScale(0.0005) 

    self.addChild(restartBtn) 
} 


func scoreLabel() { 

    let scoreDefault = NSUserDefaults.standardUserDefaults() 
    score = scoreDefault.valueForKey("score") as! NSInteger 

    scoreLbl.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 
    scoreLbl.fontSize = 40 
    scoreLbl.setScale(0.001) 
    scoreLbl.text = "Score: \(score)" 

    self.addChild(scoreLbl) 
} 


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    for touch in touches { 

     let location = touch.locationInNode(self) 

     if restartBtn.containsPoint(location) { 

      let gameScene = GameScene(fileNamed: "GameScene") 
      let transition = SKTransition.fadeWithDuration(0.5) 
      let skView = self.view! as SKView 
      skView.ignoresSiblingOrder = true 
      gameScene?.scaleMode = .AspectFill 
      skView.presentScene(gameScene!, transition: transition) 

     } else if shareBtn.containsPoint(location) { 

      // Function for sharing screenshot should be called here 
     } 
    } 

} 
+1

显示你的代码.. –

+0

@DharmeshKheni我只是包含在我的代码,以及 – Haadi

代码你没有告诉你如何使用shareScore()功能,所以我创建了一个简单的例子,你从教程,你给它的工作很好,下面是完整的示例代码:

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; 
    } 
} 

当你点击飞船图像,你会得到类似结果:

enter image description here

现在你可以轻松地分享它。

Sample更多的信息代码。

+0

谢谢您的回答。我做了一个单独的'ShareScore.swift'文件该功能,并试图调用它'如果shareBtn.containsPoint(位置)' – Haadi

+0

现在我实现您的解决方案,并是其展示这种共享弹出,但我只注意到另外一个问题它。它不需要截图,而是保存了一张没有任何内容的白色图像。 – Haadi

+0

检查示例项目。我已经测试过,它工作正常。 –