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

时间:2017-01-02 18:12:27

标签: ios objective-c instagram uidocumentinteraction

我正在尝试通过我的应用程序在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?

2 个答案:

答案 0 :(得分:2)

最后,我得到了解决方案。我们需要将图像保存在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)

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

答案 1 :(得分:0)

检查以下代码以获取Instagram上的共享图片,效果很好

导入

-(void)shareImageOnInstagram:(UIImage *)图片{     if(![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@“ instagram://”]]){         //您的设备没有Instagram应用。         返回;     }

    //__weak typeof(self) weakSelf = self;
    __block PHFetchResult *photosAsset;
    __block PHObjectPlaceholder *placeholder;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        placeholder = [createAssetRequest placeholderForCreatedAsset];
        photosAsset = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil];

    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
            requestOptions.synchronous = YES;
            PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
            allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

            NSString *identifier = placeholder.localIdentifier;
            dispatch_async(dispatch_get_main_queue(), ^{

            NSURL *instagramURL = [NSURL URLWithString:[NSString stringWithFormat:@"instagram://library?LocalIdentifier=%@",identifier]];

            if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
                [[UIApplication sharedApplication] openURL:instagramURL options:@{} completionHandler:^(BOOL success) {
                }];
            }
            });
        }
    }];

}