繁体   English   中英

如何在Instagram中分享图片?

[英]How to share image in instagram?Swift

很抱歉没有代码。但是我没有找到任何地方。 我想在instagram中分享带有标题的图片吗? 我怎样才能做到这一点?

任何帮助都会很棒

    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”类型:Array,并在这种情况下添加URL方案“ instagram”。

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

如果您不想使用UIDocumentInteractionController

SWIFT 5更新

import Photos
...

func postImageToInstagram(image: UIImage) {
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

        if let lastAsset = fetchResult.firstObject as? PHAsset {

            let url = URL(string: "instagram://library?LocalIdentifier=\(lastAsset.localIdentifier)")!

            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(alertController, animated: true, completion: nil)
            }

        }
}

在这里试试这段代码

    @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)

            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM