如何与UIActivityViewController共享图像

时间:2016-02-14 21:25:09

标签: ios swift sprite-kit

我有按钮需要共享屏幕截图并将其分享到Facebook .....等等。

分享按钮:

ShareButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 - 80,self.view!.frame.size.height/2 + 140 , 163, 54))

            ShareButton.setImage(UIImage(named: "Share-Button.png"), forState: UIControlState.Normal)

            ShareButton.addTarget(self, action: "shareButtonClicked:", forControlEvents: .TouchUpInside)

            self.view!.addSubview(ShareButton)

ShareButton func:

    func shareButtonClicked(sender: UIButton!) {

            UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
            view!.drawViewHierarchyInRect(view!.frame, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext();

            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

            let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)

            let vc = self.view?.window?.rootViewController

            //New Excluded Activities Code
            if #available(iOS 9.0, *) {
                activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
            } else {
                // Fallback on earlier versions
                activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
            }

 activityVC.popoverPresentationController?.sourceView = vc?.view
 activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
 vc?.presentViewController(activityVC, animated: true, completion: nil)

        }

当我按下按钮时,我正在收到消息控制台:

  

"其视图不在窗口层次结构中! "

并没有注意到。

工作

  • 截图保存在图库

无效

  • 显示UIActivityViewContrller并让我选择分享:Facebook ......等等。

2 个答案:

答案 0 :(得分:2)

几天后,我能够自己解决问题。

正确答案:

分享按钮:

ShareButton = UIButton.init(frame: CGRectMake(self.view!.frame.size.width/2 - 80,self.view!.frame.size.height/2 + 140 , 163, 54))

                ShareButton.setImage(UIImage(named: "Share-Button.png"), forState: UIControlState.Normal)

                ShareButton.addTarget(self, action: "pressedShareButton:", forControlEvents: .TouchUpInside)

    self.view!.addSubview(ShareButton)

ShareButton func:

    func pressedShareButton(sender: UIButton!) {

            UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
            view!.drawViewHierarchyInRect(view!.frame, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext();

            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

            let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)

            //New Excluded Activities Code
            if #available(iOS 9.0, *) {
                activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
            } else {
                // Fallback on earlier versions
                activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
            }

            activityVC.popoverPresentationController?.sourceView = view
            activityVC.popoverPresentationController?.sourceRect = ShareButton.frame
            presentViewController(activityVC, animated: true, completion: nil)


            print("Share!")    
 }

它完美无缺! 祝大家好运!

答案 1 :(得分:0)

尝试设置activityController的来源:

    activityVC.popoverPresentationController?.sourceView = vc?.view
    activityVC.popoverPresentationController?.sourceRect = ShareButton.frame