簡體   English   中英

如何使用我的ios應用程序提供的圖像打開Instagram應用程序?

[英]How to open the Instagram application with an image provided by my ios app?

我想通過我的iOS應用程序打開帶有圖像的Instagram應用程序。 我的應用程序有一個圖像URL,我想用圖像URL中包含的圖像打開Instagram頁面。

我用來打開Instagram應用程序的URL如下:

Instagram的://庫AssetPath =“+ IMAGEURL?

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

//我希望這會對你有所幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM