如何使用我的ios应用程序提供的图像打开Instagram应用程序?

时间:2019-06-07 05:11:53

标签: ios swift iphone instagram

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

我用来打开instagram应用的URL如下:

instagram:// library?AssetPath =“ + imageURL

2 个答案:

答案 0 :(得分:0)

使用以下代码在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 ")
            }
        }
    }

答案 1 :(得分:0)

//我希望这对您有帮助

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
    }
}