2017-01-02 35 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

相關問題