如何在一个应用程序中分享Instagram中的图像

时间:2022-11-13 12:17:44

I am trying to share an image on the Instagram application through my application.

我试图通过我的应用程序在Instagram应用程序上共享一个图像。

Please check my code below and please let me know where I have gone wrong.

请检查下面的代码,并告诉我出错的地方。

_instagramURL = [NSURL URLWithString:[NSString stringWithFormat: @"instagram://media?id=%@",[SingletoneClass sharedSingleTone].imageId]];

if ([[UIApplication sharedApplication] canOpenURL:_instagramURL]) {
    self.dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[SingletoneClass sharedSingleTone].imagePath]];
    self.dic.delegate = self;
    self.dic.UTI = @"com.instagram.exclusivegram";
    self.dic.annotation = [NSDictionary dictionaryWithObject:[SingletoneClass sharedSingleTone].captionStr forKey:@"InstagramCaption"];
    [self.dic presentOpenInMenuFromRect: CGRectZero inView:self.view animated:YES ];
}
else {
    UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }];
    [self presentViewController:alert animated:YES completion:nil];
}

If above code is correct, then please let me know, from where I can find the imageId?

如果上面的代码是正确的,那么请告诉我,从哪里可以找到imageId?

1 个解决方案

#1


2  

Finally, I got the solution. We need to save the image in Document directory. Instagram app can retrieve the image from Document directory.

最后,我得到了解决方案。我们需要将图像保存在Document目录中。 Instagram应用程序可以从Document目录中检索图像。

NSData *imagedata = UIImageJPEGRepresentation(image, 0.5);
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)

Then by using UIDocumentInteractionController, we can share the image in Instagram app. Before sharing the image, you need to be sure that Instagram app is present in the device.

然后通过使用UIDocumentInteractionController,我们可以在Instagram应用程序*享图像。在共享图像之前,您需要确保设备中存在Instagram应用程序。

- (void) shareImageToInstagram {
    _instagramURL = [NSURL URLWithString:[NSString stringWithFormat: @"instagram://app"]];

    if ([[UIApplication sharedApplication] canOpenURL:_instagramURL])   
    {
        self.dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[SingletoneClass sharedSingleTone].imagePath]];
        self.dic.delegate = self;
        self.dic.UTI = @"com.instagram.photo";
        self.dic.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[SingletoneClass sharedSingleTone].captionStr,@"InstagramCaption", nil];
        [self.dic presentOpenInMenuFromRect: CGRectZero inView:self.view animated:YES ];
    }
    else 
    {
        UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

You can refer the following links for more information. https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ and Share photo to Instagram from my iOS App

您可以参考以下链接以获取更多信息。 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/并从我的iOS App分享照片到Instagram

#1


2  

Finally, I got the solution. We need to save the image in Document directory. Instagram app can retrieve the image from Document directory.

最后,我得到了解决方案。我们需要将图像保存在Document目录中。 Instagram应用程序可以从Document目录中检索图像。

NSData *imagedata = UIImageJPEGRepresentation(image, 0.5);
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)

Then by using UIDocumentInteractionController, we can share the image in Instagram app. Before sharing the image, you need to be sure that Instagram app is present in the device.

然后通过使用UIDocumentInteractionController,我们可以在Instagram应用程序*享图像。在共享图像之前,您需要确保设备中存在Instagram应用程序。

- (void) shareImageToInstagram {
    _instagramURL = [NSURL URLWithString:[NSString stringWithFormat: @"instagram://app"]];

    if ([[UIApplication sharedApplication] canOpenURL:_instagramURL])   
    {
        self.dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[SingletoneClass sharedSingleTone].imagePath]];
        self.dic.delegate = self;
        self.dic.UTI = @"com.instagram.photo";
        self.dic.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[SingletoneClass sharedSingleTone].captionStr,@"InstagramCaption", nil];
        [self.dic presentOpenInMenuFromRect: CGRectZero inView:self.view animated:YES ];
    }
    else 
    {
        UIAlertController *alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@"" message:@"Instagram is not present in your device" andCallback:^(id actionResponse) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

You can refer the following links for more information. https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ and Share photo to Instagram from my iOS App

您可以参考以下链接以获取更多信息。 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/并从我的iOS App分享照片到Instagram