使用类似于SLComposeViewController的Swift将UIImage发布到Instagram
我正在做一个iOS Xcode 7 Swift 2项目。 该应用程序张贴照片到Facebook
和Twitter
使用:
var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
和
var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
爱只需将照片发布到这两个社交媒体是多么容易和简单。 我不需要或不需要他们的API。
我想为Instagram
做一些类似和简单的事情。 什么是最好的方式来做到这一点,而不必处理Instagram API
? 还是我别无select?
我的照片data
保存使用NSCoding
,而不是在DocumentDirectory
。 我在这里寻找整合,但这是一个保存在应用程序directory
的照片。
就像我已经拥有的Facebook和Twitter一样简单。
我看了看,发现一个解决scheme:
我创build了一个NSObject
:
import UIKit import Foundation class InstagramManager: NSObject, UIDocumentInteractionControllerDelegate { private let kInstagramURL = "instagram://app" private let kUTI = "com.instagram.exclusivegram" private let kfileNameExtension = "instagram.igo" private let kAlertViewTitle = "Error" private let kAlertViewMessage = "Please install the Instagram application" var documentInteractionController = UIDocumentInteractionController() // singleton manager class var sharedManager: InstagramManager { struct Singleton { static let instance = InstagramManager() } return Singleton.instance } func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, view: UIView) { // called to post image with caption to the instagram application let instagramURL = NSURL(string: kInstagramURL) if UIApplication.sharedApplication().canOpenURL(instagramURL!) { let jpgPath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(kfileNameExtension) UIImageJPEGRepresentation(imageInstagram, 1.0)!.writeToFile(jpgPath, atomically: true) let rect = CGRectMake(0,0,612,612) let fileURL = NSURL.fileURLWithPath(jpgPath) documentInteractionController.URL = fileURL documentInteractionController.delegate = self documentInteractionController.UTI = kUTI // adding caption for the image documentInteractionController.annotation = ["InstagramCaption": instagramCaption] documentInteractionController.presentOpenInMenuFromRect(rect, inView: view, animated: true) } else { // alert displayed when the instagram application is not available in the device UIAlertView(title: kAlertViewTitle, message: kAlertViewMessage, delegate:nil, cancelButtonTitle:"Ok").show() } } }
然后在我使用的@IBAction
:
let image = self.photoImageView.image InstagramManager.sharedManager.postImageToInstagramWithCaption(image!, instagramCaption: "\(self.description)", view: self.view)
这将打开一个菜单,以“开放”的风格,用户可以在Instagram
打开应用程序(如果已安装)。
更新了Swift 3
import UIKit import Foundation class InstagramManager: NSObject, UIDocumentInteractionControllerDelegate { private let kInstagramURL = "instagram://app" private let kUTI = "com.instagram.exclusivegram" private let kfileNameExtension = "instagram.igo" private let kAlertViewTitle = "Error" private let kAlertViewMessage = "Please install the Instagram application" var documentInteractionController = UIDocumentInteractionController() // singleton manager class var sharedManager: InstagramManager { struct Singleton { static let instance = InstagramManager() } return Singleton.instance } func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, view: UIView) { // called to post image with caption to the instagram application let instagramURL = NSURL(string: kInstagramURL) if UIApplication.shared.canOpenURL(instagramURL! as URL) { let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(kfileNameExtension) do { try UIImageJPEGRepresentation(imageInstagram, 1.0)?.write(to: URL(fileURLWithPath: jpgPath), options: .atomic) } catch { print(error) } let rect = CGRect(x: 0, y: 0, width: 612, height: 612) let fileURL = NSURL.fileURL(withPath: jpgPath) documentInteractionController.url = fileURL documentInteractionController.delegate = self documentInteractionController.uti = kUTI // adding caption for the image documentInteractionController.annotation = ["InstagramCaption": instagramCaption] documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true) } else { /// display some message about how it didn't work /// Like: UIAlertController(title: kAlertViewTitle, message: kAlertViewMessage, preferredStyle: .alert) } } }
然后在ViewController中你想调用它…
InstagramManager.sharedManager.postImageToInstagramWithCaption(imageInstagram: <YOUR IMAGE>, instagramCaption: shareText, view: self.view)
如果安装了Instagram,这将打开“打开”function