通过您自己的iOS应用上传照片到Instagram

Instagram最近对自己的API政策进行了修改,允许开发者通过自己的应用将照片发布到Instagram平台上。 我们以前采用了其他几种技术来做到这一点。 其中之一就是调用Instagram应用程序,从本质上开放Instagram并从那里分享。 一个关于如何做到这一点的教程可以在这里看到: 如何从你自己的iOS应用程序共享图像到Instagram

然而,有几个应用程序允许直接共享到Instagram平台,而无需调用Instagram应用程序。 Hipstamatic的Oggl允许直接分享到Instagram,而无需调用Instagram。 下面我张贴了一些关于这个过程的截图。

一旦我的照片被拍摄,奥格尔给了我几个其他的社交networking,我可以分享我的照片。 我select了Facebook和Instagram。

在这里输入图像说明

在我select了Instagram之后,它开启了Safari,它将我带到以下两页,授权Oggl发布到Instagram。 我input了我的Instagram凭据,然后它将我带到授权页面。

在这里输入图像说明在这里输入图像说明

一旦我授权Oggl,我就能够上传到Instagram,几秒钟之内,我看到了我的Instagram新闻提要上的照片。 这种分享类似于Facebook和Twitter分享。 它有相同的概念。 怎么能这样做? 如何才能在他们的应用程序中复制这个确切的过程? 在我的应用程序中拍摄的照片是612px×612px ,因此它们与在Instagram上拍摄的照片的尺寸兼容。 我已经实现了分享到Facebook和Twitter,但我想实现上传到Instagram就像Oggl做的。 这可能吗?

有许多iOS开发人员可以从这个问题的详细规范的答案中受益。

谢谢

Hipstamatic是通过Instagram的API获得写入权限的less数应用程序之一。

Instagram没有张贴照片的公开API,它可以让你得到他们的数据,但不写你自己的。

Hipstamatic和其他几个应用程序已经与Instagram达成了特别协议,允许他们使用写入API并直接发布照片。

对于所有其他开发者,您需要使用iOS钩子通过切换到他们的应用程序来分享到Instagram。

据我所知,让Instagram写访问并不容易 – 你需要有一个高质量的内容的顶级应用程序,并与合适的人友好:)

你不能直接在Instagram上发布图片。 你必须redirect你的图像与UIDocumentInteractionController。 使用下面的代码,希望它会有所帮助

 @property (nonatomic, retain) UIDocumentInteractionController *dic; CGRect rect = CGRectMake(0 ,0 , 0, 0); UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIGraphicsEndImageContext(); NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"]; NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]]; self.dic.UTI = @"com.instagram.photo"; self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self]; self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile]; [self.dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ]; -(UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate { UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL]; interactionController.delegate = interactionDelegate; return interactionController; } 

你正在试图做的是通过Instagram API http://instagram.com/developer/实&#x73B0; 。 您也可以使用Instagram应用程序来执行一些类似的操作。 这些都logging在iPhone挂钩下 。 如果您使用的是Ruby Motion,那么您将可以使用官方框架 。 不幸的是,没有官方支持的Objective-C iOS API,但是有一些开源替代品可用,比如NRGramKit 。

实现与Instagram API交互的确切方式超出了堆栈溢出的答案,但如果你熟悉iOS编程,上面的链接应该给你一个很好的起点。

从Instagram API文档逐字:

https://instagram.com/developer/endpoints/media/

目前,通过API上传是不可能的。 我们有意识地select不添加这个,原因如下:

  1. Instagram是关于你在旅途中的生活 – 我们希望鼓励在应用程序内的照片。
  2. 我们想打击垃圾邮件和低质量的照片。 一旦我们允许从其他来源上传,就很难控制进入Instagram生态系统的内容。 所有这一切,我们正在努力确保用户在我们的平台上获得一致和高质量的体验。

如果你想只发送图像而不削减你应该设置图像大小640×640或应用程序崩溃

这是我的swift代码

  var instagramURL = NSURL(string: "instagram://app")! if UIApplication.sharedApplication().canOpenURL(instagramURL) { var documentDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents") var saveImagePath = documentDirectory.stringByAppendingPathComponent("Image.igo") var imageData = UIImagePNGRepresentation(self.bestScoreShareView.takeSnapshot(W: 640, H: 640)) imageData.writeToFile(saveImagePath, atomically: true) var imageURL = NSURL.fileURLWithPath(saveImagePath)! docController = UIDocumentInteractionController() docController.delegate = self docController.UTI = "com.instagram.exclusivegram" docController.URL = imageURL docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true) } else { println("instagram not found") } 

从uview中取shapshot的扩展名

 extension UIView { func takeSnapshot(#W: CGFloat, H: CGFloat) -> UIImage { var cropRect = CGRectMake(0, 0, 600, 600) UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale) drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() image.imageWithNewSize(CGSizeMake(W, H)) return image } } 

设置图像大小的扩展名

 extension UIImage { func imageWithNewSize(newSize:CGSize) ->UIImage { UIGraphicsBeginImageContext(newSize) self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage } } 

您可能还想查看iPhone的Hooks的Instagram文档的“文档交互”部分 。 这可以用来做你正在做的事情,这可能是Oggl做的。

我已经使用下面的代码在Instagram中分享照片。

  NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not { NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"insta.igo"]]; //add our image to the path [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) NSLog(@"image saved"); CGRect rect = CGRectMake(0 ,0 , 0, 0); UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIGraphicsEndImageContext(); NSString *fileNameToSave = [NSString stringWithFormat:@"Documents/insta.igo"]; NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave]; NSLog(@"jpg path %@",jpgPath); NSString *newJpgPath = [NSString stringWithFormat:@"file://%@",jpgPath]; NSLog(@"with File path %@",newJpgPath); NSURL *igImageHookFile = [[NSURL alloc]initFileURLWithPath:newJpgPath]; NSLog(@"url Path %@",igImageHookFile); self.documentController.UTI = @"com.instagram.exclusivegram"; // self.documentController = [self setupControllerWithURL:igImageHookFile usingDelegate:self]; self.documentController=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile]; NSString *caption = @"#Your Text"; //settext as Default Caption self.documentController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",caption],@"InstagramCaption", nil]; [self.documentController presentOpenInMenuFromRect:rect inView: self.view animated:YES]; } else { UIAlertView *errMsg = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"No Instagram Available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [errMsg show]; }