2017-01-02 77 views
2

我想通过我的应用程序在Instagram应用程序中共享图像。如何通过一个应用程序在Instagram中共享图像

请检查我的代码下面,请让我知道我错了什么地方。

_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]; 
} 

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

+0

您可以添加关于SingletonClass文件的描述。它返回什么对象。你可能会问: –

+0

@AnilGupta询问, UIAlertController * alert = [[SingletoneClass sharedSingleTone] setAlertControllerWithTitle:@“”message:@“Instagram is not in your device”andCallback:^(id actionResponse){alertdismissViewControllerAnimated:YES completion :零]; }]; –

+0

如果您要从Photo Gallary或Images资源共享图像,而不是简单地将图像对象路径传递到“_instagramURL”中。您可以通过注册您的applcation从https://www.instagram.com/developer/找到imges ID。但分享你不需要注册你的应用程序。 –

回答

3

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

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) 

然后通过使用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]; 
    } 
} 

您可以参考以下链接了解更多信息。 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/Share photo to Instagram from my iOS App

+0

你能告诉我什么是单身人士课堂内?请上传完整的代码。 – Abha

相关问题