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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18242610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:43:46  来源:igfitidea点击:

Uploading photos to Instagram via your own iOS app

iosinstagram

提问by deadlock

Instagram recently made a change to their API policy which allowed developers to post pictures to the Instagram platform via their own app. Several other techniques we're previously employed to do this. One of them was to invoke the Instagram application which would essentially open up Instagram and do the sharing from there. A tutorial on how this can be done can be seen here: How to share image to Instagram from your own iOS app

Instagram 最近更改了他们的 API 政策,允许开发人员通过他们自己的应用程序将图片发布到 Instagram 平台。我们以前使用过的其他几种技术来做到这一点。其中之一是调用 Instagram 应用程序,该应用程序基本上会打开 Instagram 并从那里进行共享。可以在此处查看有关如何完成此操作的教程:如何从您自己的 iOS 应用程序将图像共享到 Instagram

However there are several applications out there that allow for direct sharing to the Instagram platform without invoking the Instagram application. Hipstamatic's Ogglallows for direct sharing to Instagram without invoking Instagram. Below I have posted some screen shots of the process.

但是,有几个应用程序允许直接共享到 Instagram 平台,而无需调用 Instagram 应用程序。Hipstamatic 的 Oggl允许在不调用 Instagram 的情况下直接分享到 Instagram。下面我发布了一些过程的屏幕截图。

Once my picture was taken, Oggl gave me several other social networks to which I could share my photo to. I've selected Facebook and Instagram.

拍完照片后,Oggl 给了我其他几个社交网络,我可以将照片分享到这些社交网络。我选择了 Facebook 和 Instagram。

enter image description here

在此处输入图片说明

After I selected Instagram, it opened up Safari and it brought me to the following two pages to authorize Oggl to post to Instagram. I entered in my Instagram credentials and then it brought me to the authorization page.

在我选择 Instagram 后,它打开了 Safari,它把我带到了以下两个页面,授权 Oggl 发布到 Instagram。我输入了我的 Instagram 凭据,然后它把我带到了授权页面。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Once I authorized Oggl, I was able to upload to Instagram and within seconds, I saw the photo on my Instagram news feed. This type of sharing is very analogous to Facebook and Twitter sharing. It has the same concept. How can one go about doing this?How can one replicate this exact process in their app? The pictures taken in my application are 612px by 612 px, so they are compatible with the dimensions for photos taken on Instagram. I've already implemented sharing to Facebook and Twitter but I would like to implement uploading to Instagram just like how Oggl did. Is this possible?

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

There are many iOS developers out there who can benefit from a well detailed canonical answer to this question.

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

Thank You

谢谢你

采纳答案by Rowan

Hipstamatic are one of the few apps that has been given write access via Instagram's API.

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

Instagram doesn't have a public API for posting photos, it lets you get their data but not write your own.

Instagram 没有用于发布照片的公共 API,它允许您获取他们的数据但不能编写自己的数据。

Hipstamatic and several other apps have negotiated special deals with Instagram that allow them to use the write API and post photos directly.

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

For all other developers you need to use iOS hooks to share to Instagram by switching to their app.

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

As far as I know its not easy to get Instagram to allow you write access- you would need to have a top app with high quality content and be friendly with the right people :)

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

回答by Faran Ghani

You can not directly post an image on Instagram. You have to redirect your image with UIDocumentInteractionController. Use Below code , Hope It will help

您不能直接在 Instagram 上发布图片。您必须使用 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;
}

回答by NiloVelez

verbatim from the Instagram API documentation:

Instagram API 文档中的逐字逐句:

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

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

At this time, uploading via the API is not possible. We made a conscious choice not to add this for the following reasons:

  1. Instagram is about your life on the go – we hope to encourage photos from within the app.
  2. We want to fight spam & low quality photos. Once we allow uploading from other sources, it's harder to control what comes into the Instagram ecosystem. All this being said, we're working on ways to ensure users have a consistent and high-quality experience on our platform.

目前,无法通过 API 上传。由于以下原因,我们有意识地选择不添加此项:

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

回答by Beslan Tularov

if you want to just send the image without clipping you should set image size of 640 x 640 or the application crash

如果您只想发送图像而不剪裁,您应该将图像大小设置为 640 x 640 或应用程序崩溃

This my swift code

这是我的快速代码

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

extension for take shapshot from uiview

从 uiview 拍摄快照的扩展

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 for set image size

设置图像大小的扩展

extension UIImage {
     func imageWithNewSize(newSize:CGSize) ->UIImage {
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))

        let newImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        return newImage
    }
}

回答by allprog

What you are trying to do is implemented through the Instagram API http://instagram.com/developer/. You can also use the Instagram App to do some similar actions. These are documented under iPhone Hooks. If you are using Ruby Motion, then you will be able to use the official framework. Unfortunately, there is no officially supported Objective-C iOS API but some open source alternatives are available, like the NRGramKit.

您要做的是通过 Instagram API http://instagram.com/developer/ 实现。您还可以使用 Instagram 应用程序执行一些类似的操作。这些记录在iPhone Hooks 下。如果您使用的是 Ruby Motion,那么您将能够使用官方框架。不幸的是,没有官方支持的 Objective-C iOS API,但有一些开源替代品,比如NRGramKit

The exact way of implementing the interaction with the Instagram API is beyond a Stack Overflow answer but the links above should give you a good starting point if you are familiar with iOS programming.

实现与 Instagram API 交互的确切方法超出了 Stack Overflow 的答案,但如果您熟悉 iOS 编程,上面的链接应该为您提供一个很好的起点。

回答by omdel

You may also want to check out the "Document Interaction" section of Instagram's API documentation for iPhone Hooks. This could be leveraged to do what you are trying to do and it might be the way Oggl does it.

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

回答by Nirzar Gandhi

I have used below code for sharing photo in Instagram.

我使用以下代码在 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];
    }