将照片导出到Instagram

时间:2013-01-19 18:34:19

标签: iphone ios xcode instagram uidocument

我正在尝试使用以下代码将照片从我的应用程序导出到Instagram:

// Capture Screenshot
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,self.view.opaque,0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
dic.UTI = @"com.instagram.photo";
dic.annotation = [NSDictionary dictionaryWithObject:@"my caption" forKey:@"InstagramCaption"];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
  [[UIApplication sharedApplication] openURL:instagramURL];
} else {
  NSLog(@"No Instagram Found");
}

但它不起作用。它崩溃时出现以下错误:

  

2013-01-19 20:40:41.948 Ayat [12648:c07] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [NSURL initFileURLWithPath:]:nil string参数'

我不确定我是否正确:

1-保存jpgpath作为我拍摄的截图。

2-将“dic”文档传递给Instagram。

4 个答案:

答案 0 :(得分:12)

在我的应用程序中,我还将照片导出到Instagram。 (图片大小必须大于612x612) 试试这段代码:

-(void)shareImageOnInstagram:(UIImage*)shareImage
{
    //Remember 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 not found");

    }

}

我希望这会对你有所帮助。

答案 1 :(得分:2)

在这一行

dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]];
你打电话给fileURLWithPath:。首先,这需要NSString,而不是NSURL,但是从您的错误消息中看来igImageHookFilenil

答案 2 :(得分:0)

我花了很多研究但最终在这个网站上找到了这个帮助。我只是把它修剪了一下,不包括标题,因为那是他原来的问题......但如果你只是放在画面中,这里是:

-(void)shareImageOnInstagram
{
//Remember Image must be larger than 612x612 size if not resize it.
 NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]){
    NSString *urlString = [[NSString alloc] initWithFormat:@"file://%@", blendedImageURL];
    NSURL *imageUrl = [[NSURL alloc] initWithString: urlString];
    self.docController = [self setupControllerWithURL:imageUrl usingDelegate:self];
    self.docController.UTI = @"com.instagram.exclusivegram";
   // self.docController.annotation = [NSDictionary dictionaryWithObject:@"I want to     share this text" forKey:@"InstagramCaption"];
    [self.docController presentOpenInMenuFromRect: self.view.frame inView: self.view     animated: YES ];}
}

答案 3 :(得分:0)

我尝试了以下代码并且它有效:

- (void) shareImageWithInstagram
{
    NSURL *instagramURL = [NSURL URLWithString:@"instagram://"];
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
    {
        UICachedFileMgr* mgr = _gCachedManger;
        UIImage* photoImage = [mgr imageWithUrl:_imageView.image];
        NSData* imageData = UIImagePNGRepresentation(photoImage);
        NSString* captionString = [NSString stringWithFormat:@"%@%@",self.approvedData.approvedCaption,self.approvedData.tag];
        NSString* imagePath = [UIUtils documentDirectoryWithSubpath:@"image.igo"];
        [imageData writeToFile:imagePath atomically:NO];
        NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"file://%@",imagePath]];

        self.docFile = [[self setupControllerWithURL:fileURL usingDelegate:self]retain];
        self.docFile.annotation = [NSDictionary dictionaryWithObject: captionString
                                                         forKey:@"InstagramCaption"];
        self.docFile.UTI = @"com.instagram.photo";

        // OPEN THE HOOK
        [self.docFile presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
    }
    else
    {
        [UIUtils messageAlert:@"Instagram not installed in this device!\nTo share image please install instagram." title:nil delegate:nil];
    }
}