【问题标题】:How to export an Image from our App to Instagram?如何将图像从我们的应用程序导出到 Instagram?
【发布时间】:2013-02-05 12:32:01
【问题描述】:

我使用了以下代码,但它不起作用。 我想将图像从我的应用程序放到我 iPhone 上安装的 Instagram 应用程序中。

NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"IMG_0192"  ofType:@"jpg"];
fileToOpen = [fileToOpen substringToIndex:[fileToOpen length] - 3];
fileToOpen=[NSString stringWithFormat:@"%@ig",fileToOpen];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
NSLog(@"%@",fileToOpen);
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
//imageToUpload is a file path with .ig file extension
/*UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"" 
                      message:@"Can open app!" 
                      delegate:self  
                      cancelButtonTitle:@"OK" 
                      otherButtonTitles:nil];
[alert show];
*/

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileToOpen]];
self.documentInteractionController.UTI = @"com.instagram.photo";
self.documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Its a testing" forKey:@"InstagramCaption"];

我尝试了很多,但它根本不起作用。

【问题讨论】:

    标签: iphone ios objective-c instagram


    【解决方案1】:

    您可以使用以下代码将图像从您的应用分享到安装在您 iPhone 上的 Instagram 应用。

    - (void) shareImageToInstagram 
    {
        NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];
        if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) 
        {
            CGRect rect = CGRectMake(0,0,0,0);
            CGRect cropRect=CGRectMake(0,0,612,612);
            NSString *jpgPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];
            CGImageRef imageRef = CGImageCreateWithImageInRect([imgViewPost.image CGImage], cropRect);
            UIImage *img = [[UIImage alloc] initWithCGImage:imageRef];
            CGImageRelease(imageRef);
    
            [UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
            NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@",jpgPath]];
            self.dicont.UTI = @"com.instagram.photo";
            self.dicont = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
            self.dicont.annotation = [NSDictionary dictionaryWithObject:@"Your AppName" forKey:@"InstagramCaption"];
            [self.dicont presentOpenInMenuFromRect: rect  inView: self.view animated: YES ];
        }
        else
        {
            DisplayAlert(@"Instagram not installed in this device!\nTo share image please install instagram.");
        }
    }
    
    - (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
        UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
        interactionController.delegate = interactionDelegate;
    
        return interactionController;
    }
    

    请试试这个代码..

    【讨论】:

    • 这会将图像从您的应用程序打开到您设备上安装的 Instagram 应用程序。
    • 我似乎无法将注释转移到 Instagram - 标题只是空的。有人有这方面的经验吗?
    【解决方案2】:

    您可以使用此代码:

    -(void)shareImageOnInstagram:(UIImage*)shareImage
    {
       //It is important that image must be larger than 612x612 size if not resize it.   
    
        NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
    
        if([[UIApplication sharedApplication] canOpenURL:instagramURL])
        {
            NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
            NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.ig"];
            NSData *imageData=UIImagePNGRepresentation(shareImage);
            [imageData writeToFile:saveImagePath atomically:YES];
    
            NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
    
            UIDocumentInteractionController *docController=[[UIDocumentInteractionController alloc]init];
            docController.delegate=self;
            [docController retain];
            docController.UTI=@"com.instagram.photo";
    
            docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:@"Image Taken via @App",@"InstagramCaption", nil];
    
            [docController setURL:imageURL];
    
    
            [docController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];  //Here try which one is suitable for u to present the doc Controller. if crash occurs
        }
        else
        {
            NSLog ("Instagram is not available");
        }
    }
    

    【讨论】:

    • 我似乎无法将注释转移到 Instagram - 标题只是空的。有人有这方面的经验吗?
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2019-09-08
    相关资源
    最近更新 更多