iphone - 将照片导出到 Instagram

标签 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] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

我不确定我是否正确:

1-将jpgpath保存为我截取的截图。

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

最佳答案

在我的应用程序中,我还将照片导出到 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");

    }

}

希望对您有所帮助。

关于iphone - 将照片导出到 Instagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14417459/

相关文章:

iPhone SDK : How to add an outline to UILabel's text

ios/iphone - [NSNull dataUsingEncoding :]: unrecognized selector sent to instance

ios - 上传图像/视频到 FTP Swift 3.0

iphone - 使用 xmpp 框架时未调用委托(delegate)方法

ios - 如何处理通知服务扩展崩溃导致的崩溃?

javascript - 在 HTML5 iOS 7/iOS 8 中显示十进制键盘

iphone - toSharedViewController 不重用现有的 Controller

objective-c - iTunes Connect 不要求更新屏幕截图?

ios - 将ios 6.1和xcode升级到4.6时出错

iphone - 关于使用 RegexKitLite 进行正则表达式评估的问题