在活动视图控制器中共享屏幕截图 - Swift

时间:2015-01-13 04:55:14

标签: swift share screenshot uiactivityviewcontroller

我正在开发应用程序的一部分,以便当您点按分享按钮时,它允许您立即分享您的高分榜的屏幕截图以及消息。我无法制作/分享屏幕截图,当我点击分享按钮时,应用程序只允许我复制我的默认文本或“邮件”我的默认文本,不允许我发布到Facebook,Twitter,消息等等。

func shareButtonPress() {

    var postPhrase = "Just hit \(highscore)! Beat it! #SwypIt"

    //Generate the screenshot
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

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

    var activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [postPhrase, postImage!], applicationActivities: nil)

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

解决这个问题的最佳方式是什么?谢谢!

2 个答案:

答案 0 :(得分:10)

这就是我在我的应用中处理共享的方式。

    func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
    var sharingItems = [AnyObject]()

    if let text = sharingText {
        sharingItems.append(text)
    }
    if let image = sharingImage {
        sharingItems.append(image)
    }
    if let url = sharingURL {
        sharingItems.append(url)
    }

    let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
    self.presentViewController(activityViewController, animated: true, completion: nil)
}

我已使用.excludedActvityTypes排除了许多共享选项。

然后,无论何时按下分享按钮,都会调用此

socialShare(sharingText: "Just hit \(highscore)! Beat it! #SwypI", sharingImage: UIImage(named: "The screenshot you are saving"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))

您没有将Twitter和Facebook视为共享选项的原因是因为您需要在iPhone上的设置中登录它们。不是个别应用程序。

希望这有帮助。

答案 1 :(得分:0)

使用SLComposeViewController。

 import Social

 func shareButtonPress() {

   var postPhrase = "New high score \(highscore)!"

   //Generate the screenshot
   UIGraphicsBeginImageContext(view.frame.size)
   view.layer.renderInContext(UIGraphicsGetCurrentContext())
   var image = UIGraphicsGetImageFromCurrentImageContext()
   UIGraphicsEndImageContext()

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

   let shareToFacebook = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
   shareToFacebook.setInitialText(postPhrase)
   shareToFacebook.addImage(postImage)
   presentViewController(shareToFacebook, animated: true, completion: nil)
 }

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/SLComposeViewController_Class/