【问题标题】:How to open the Instagram application with an image provided by my ios app?如何使用我的 ios 应用程序提供的图像打开 Instagram 应用程序?
【发布时间】:2019-06-07 05:11:53
【问题描述】:

我想通过我的 iOS 应用打开带有图像的 Instagram 应用。我的应用程序有一个图片 URL,我想用图片 URL 中包含的图片打开 Instagram 页面。

我用来打开 Instagram 应用程序的 URL 如下所示:

instagram://library?AssetPath="+imageURL

【问题讨论】:

    标签: ios swift iphone instagram


    【解决方案1】:

    使用以下代码在 Instagram 分享图片,希望对您有所帮助。

    首先从您的图片 URL 获取图片(下载),并在此函数中用作图片(下载的图片)。

    @IBAction func shareImageInstagram(_ 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】:

      //希望对你有帮助

      func shareImage(_ image: UIImage?, withCaption caption: String?) {
          var img: UIImage? = image
          let instagramURL: URL = URL(string: "instagram://")!
          if UIApplication.shared.canOpenURL(instagramURL) == true {
              if img != nil {
                  let jpgPath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")
                  do {
                      try UIImageJPEGRepresentation(img!, 1.0)!.write(to: URL(string: jpgPath)!, options: .atomic)
                  } catch _ {
                  }
                  //writeToFile(jpgPath, atomically:true)
                  let rect = CGRect(x: 0, y: 0, width: 0, height: 0)
                  let fileURL: URL = URL(fileURLWithPath: jpgPath)
                  let docVc = UIDocumentInteractionController(url: fileURL)
                  docVc.uti = "com.instagram.exclusivegram"
                  if caption != nil && caption?.count ?? 0 > 0 {
                      docVc.annotation = NSDictionary(object: caption!, forKey: "InstagramCaption" as NSCopying)
                  }
      
                  docVc.presentOpenInMenu(from: rect, in: self.view, animated: true)
              }
          } else {
              // please install instagram
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-08
        • 2013-06-18
        • 1970-01-01
        • 2017-02-10
        • 2013-03-11
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多