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

Sharing A Screenshot in Activity View Controller - Swift

我正在开发一个应用程序的一部分,这样当您点击分享按钮时,它可以让您立即分享您的高分屏幕截图以及一条消息。我无法 produce/share 截图,当我点击分享按钮时,该应用程序只允许我复制我的默认文本或 "Mail" 我的默认文本,不允许我 post 到 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)

}

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

使用 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/

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

    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 的设置中登录它们。不是个别应用程序。

希望对您有所帮助。