如何在Instagram中分享图片?Swift

时间:2015-07-10 09:38:48

标签: ios swift instagram

很抱歉没有代码的问题。但我找不到任何地方可以找。 我想在Instagram分享图片与标题?我怎么能这样做?

任何帮助都会很棒

4 个答案:

答案 0 :(得分:12)

    class viewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var yourImage: UIImage?
    var documentController: UIDocumentInteractionController!

    func shareToInstagram() {

     let instagramURL = NSURL(string: "instagram://app")

            if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {

                let imageData = UIImageJPEGRepresentation(yourImage!, 100)

                let captionString = "caption"

           let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")
           if imageData?.writeToFile(writePath, atomically: true) == false {

                    return

                } else {
   let fileURL = NSURL(fileURLWithPath: writePath)

                    self.documentController = UIDocumentInteractionController(URL: fileURL)

                    self.documentController.delegate = self

                    self.documentController.UTI = "com.instagram.exlusivegram"

                    self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
                          self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)

                }

            } else {
                print(" Instagram isn't installed ")
            }
        }
     }

    }

现在这仍然不适用于iOS 9,所以你必须转到你的应用程序info.plist,添加“LSApplicationQueriesSchemes”类型:数组,并在这种情况下添加URL方案“instagram”。

答案 1 :(得分:10)

Swift 3.0 版本:

 @IBAction func shareInstagram(_ sender: Any) {

        DispatchQueue.main.async {

            //Share To Instagram:
            let instagramURL = URL(string: "instagram://app")
            if UIApplication.shared.canOpenURL(instagramURL!) {

                let imageData = UIImageJPEGRepresentation(image, 100)
                let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo")

                do {
                    try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic)
                } catch {
                    print(error)
                }

                let fileURL = URL(fileURLWithPath: writePath)
                self.documentController = UIDocumentInteractionController(url: fileURL)
                self.documentController.delegate = self
                self.documentController.uti = "com.instagram.exlusivegram"

                if UIDevice.current.userInterfaceIdiom == .phone {
                    self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true)
                } else {
                    self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true)
                }
            } else {
                print(" Instagram is not installed ")
            }
        }
    }

答案 2 :(得分:9)

如果您不想使用UIDocumentInteractionController

<td><?php echo (int)$row["S1"] ?></td>
<td><?php echo ($row["S1"] - (int)$row["S1"])*100 ?></td>

答案 3 :(得分:3)

在这里试试这段代码

    @IBAction func shareContent(sender: UIButton) {

              let image = UIImage(named: "imageName")
            let objectsToShare: [AnyObject] = [ image! ]
            let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view 


            activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]


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

            }
        }