簡體   English   中英

Instagram iPhone分享圖片

[英]Instagram iPhone share image

我正在嘗試分享圖片到Instagram。 文檔( https://www.instagram.com/developer/mobile-sharing/iphone-hooks/ )僅針對show Instagram介紹UTI“com.instagram.exclusivegram”和圖片擴​​展名“igo”。 但是UIDocumentInteractionController為我顯示了很多選項。 不僅是Instagram。 有沒有辦法直接分享到Instagram? (請參閱UIDocumentInteractionController中的單個選項)

UIDocumentInteractionController *docInteraction=[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:@".../image.igo"]];
docInteraction.UTI=@"com.instagram.exclusivegram";
BOOL presented=[docInteraction presentOpenInMenuFromRect:CGRectMake(0, self.view.frame.size.height-1, self.view.frame.size.width, 1)
                                                  inView:self.view
                                                animated:YES];

截圖

使用下面的課程在Instagram上分享它是100%工作。

文件名:InstagramSharing.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface InstagramSharing : NSObject <UIDocumentInteractionControllerDelegate>
+ (InstagramSharing*)sharedData;

@property (nonatomic, retain) UIDocumentInteractionController *documentController;

-(void)instaGramWallPost:(NSString *)imgName andExt:(NSString *)ext inViewController:(UIViewController *)viewController;

@end

文件名:InstagramSharing.m

#import "InstagramSharing.h"


@implementation InstagramSharing
+ (id) sharedData
    {
        static InstagramSharing *singletonInstance = nil;
        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{
            if(!singletonInstance) {
                singletonInstance = [[InstagramSharing alloc] init];
            }
        });

        return singletonInstance;
    }

-(NSString*) getFilePath:(NSString *)fileName{
        //    NSString *searchFilename = @"hello.pdf"; // name of the PDF you are searching for

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

        NSString *documentsSubpath;
        while (documentsSubpath = [direnum nextObject])
        {
            if (![documentsSubpath.lastPathComponent isEqual:fileName]) {
                continue;
            }

            NSLog(@"found %@", documentsSubpath);
        }
        return documentsSubpath;
    }

-(void)instaGramWallPost:(NSString *)imgName andExt:(NSString *)ext inViewController:(UIViewController *)viewController
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:imgName  ofType:ext];
        NSURL *myURL = [NSURL fileURLWithPath:path];

        NSData * imageData = [[NSData alloc] initWithContentsOfURL:myURL];
        UIImage *imgShare = [[UIImage alloc] initWithData:imageData];

        NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];

        if([[UIApplication sharedApplication] canOpenURL:instagramURL]) //check for App is install or not
        {
            UIImage *imageToUse = imgShare;
            NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
            NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.igo"];
            NSData *imageData=UIImagePNGRepresentation(imageToUse);
            [imageData writeToFile:saveImagePath atomically:YES];
            NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];

            NSString *path = [[NSBundle mainBundle] pathForResource:imgName ofType:ext];
            NSURL *url = [NSURL fileURLWithPath:path];

            self.documentController=[[UIDocumentInteractionController alloc]init];
            self.documentController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
            self.documentController.delegate = self;
            self.documentController.annotation = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Testing"], @"InstagramCaption", nil];
            self.documentController.UTI = @"com.instagram.exclusivegram";
            UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
            [self.documentController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:viewController.view animated:YES];
        }
        else {
            //            DisplayAlertWithTitle(@"Instagram not found", @"")
            NSLog(@"no image found");
        }
    }



@end

如何使用

在線下的股票行動電話

[[InstagramSharing sharedData] instaGramWallPost:@"nature1" andExt:@"jpeg" inViewController:self];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM