在共享表中共享屏幕截图

时间:2015-04-24 08:36:09

标签: swift sharing

我正在制作一个类似游戏的游戏,当玩家输掉时我会给他们通过iOS共享表分享他们的分数的选项。我想知道的是,我怎么能让他们分享截图,当他们与一些文本一起死时。我已经知道如何制作它以便他们共享文本但我也想要截图。我将其设置为这样,以便游戏在玩家死亡时立即截屏:

func screenShotMethod() {
    //Create the UIImage
    UIGraphicsBeginImageContext(view!.frame.size)
    view!.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

    println("screenshot")
}

然后我在GameOver序列中运行这个函数,如下所示:

    if gameOver == 0{

gameOver = 1

***screenShotMethod()***

movingObjects.speed = 0

movingObjects.removeFromParent()

backgroundMusicPlayer.stop()

现在我想要做的是访问此屏幕截图,以便它可以在共享选项中使用,但如果玩家没有分享该分数,则在玩家重播时立即删除。现在我有这样的分享设置:

if shareButton.containsPoint(location){

        UIGraphicsBeginImageContext(view!.frame.size)
        view!.layer.renderInContext(UIGraphicsGetCurrentContext())
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        //Save it to the camera roll
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

        println("screenshot")

        var postImage = UIImage(named: "\(image)")

        socialShare(sharingText: "I just got \(score) points in Deez Nuts! Bet you can't beat that! #DeezNuts", sharingImage: UIImage(named: "\(postImage)"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))


    }

请具体而直截了当,因为我是开发应用程序的新手。如果你还没注意到我也在使用Swift。非常感谢你。

1 个答案:

答案 0 :(得分:1)

您只需删除此行

即可
var postImage = UIImage(named: "\(image)")

因为图像已经是UIImage所以只需使用sharingImage:image

socialShare(sharingText: "I just got \(score) points in Deez Nuts! Bet you can't beat that! #DeezNuts", sharingImage: image , sharingURL: NSURL(string: "http://itunes.apple.com/app/")!)