将图片导出到Instagram - iPhone

4

我有一张图片,想要将其导出到Instagram,以便发布。

我使用的代码是:

NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image.igo"];

    UIImage *image = [UIImage imageNamed:@"01.png"];

    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:YES];        
    NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath];
    NSLog(@"%@",imageUrl);
    UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];        
    docController.delegate = self;
    docController.UTI = @"com.instagram.exclusivegram";
    docController.URL = imageUrl;
    //[docController setURL:imageUrl];
    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
}

当我运行应用程序时,应用程序显示一个名为“Instagram”的按钮及其图标,但当我点击它时,按钮消失了,什么都没有发生。应用程序没有崩溃,但是什么也没发生。
我在代码中错过了什么?
谢谢 Bruno

你能具体说明一下“什么都没发生”吗?当你调试它时,是否所有的代码都执行了,只是你看不到文档交互控制器?还是其他什么地方出了问题? - Carl Veazey
Carl,所有的代码都已执行,但没有任何反应...该图像为612 x 612像素,并且NSLog显示URL: URL = file://localhost/var/mobile/Applications/96007767-1D2E-4017-9EEA-072F3CE19452/Documents/Image.ig - Brunohdc
尝试一个不同的 -presentOpenInMenuFromRect:inView:Animated: 的第一个参数。或许可以尝试发送按钮的框架? - Carl Veazey
你尝试过使用 UTI 的 com.instagram.photo 和 .ig 扩展名了吗?委托方法是否被调用了? - Felix
1个回答

3
我猜问题出在您没有保留UIDocumentInteractionController。请在类中创建一个ivar变量。
确保代理方法documentInteractionController:didEndSendingToApplication:被调用。
另外,请查看Instagram文档:http://instagram.com/developer/iphone-hooks/ 引用块: 当触发时,Instagram将立即向用户展示我们的滤镜屏幕。图像已经预加载并且已适当缩放为Instagram。除了使用上述描述的适当图像格式之外,我们唯一的要求是图像至少为612px高和/或宽。为了获得最佳效果,Instagram更喜欢打开一个612x612像素正方形的JPEG。如果图像较大,则会动态调整大小。
要验证是否安装了Instagram,请检查URL instagram://app。URL instagram://location?id=LOCATION_ID仅用于位置供稿!

1
Phix23,它起作用了!非常感谢!!!

我会在这里发布以帮助任何遇到相同问题的人。

在.h文件中 添加<UIDocumentInteractionControllerDelegate>

@property (nonatomic, retain) UIDocumentInteractionController *docController;

- Brunohdc
感谢Phix23和Brunohdc.....我正在使用ARC..它不允许我放置[docController retain]; 我该怎么办..但是如果我在编译器标志中放置-fno-objc-arc,它可以正常工作..但我想在ARC中使用它..请帮帮我..提前致谢... - Ashok
1
@AshokKumarIOS 使用ARC只需为docController创建一个ivar或strong属性。然后删除retain即可。 - Felix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接