0

我有以下代码可以将照片发布到 Instagram:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* homePath = [paths objectAtIndex:0];
    NSString* basePath = @"integration/instagram";
    NSString* tmpFileName = @"jumpto.ig";

    NSString* dirPath = [NSString stringWithFormat:@"%@/%@", homePath, basePath];
    NSString* docPath = [NSString stringWithFormat:@"%@/%@", dirPath, tmpFileName];

    //clear it out and make it fresh
    [[NSFileManager defaultManager] removeItemAtPath:docPath error:nil];
    if ([[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]) {

        NSData* imgData = [self generateImageData:img];
        [[NSFileManager defaultManager] createFileAtPath:docPath contents:imgData attributes:nil];
        NSURL* url = [NSURL fileURLWithPath:docPath isDirectory:NO ];

        NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"];
        if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
            //imageToUpload is a file path with .ig file extension
            self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
            self.documentInteractionController.UTI = @"com.instagram.exclusivegram";
            self.documentInteractionController.delegate = self;
            self.documentInteractionController.annotation = [NSDictionary dictionaryWithObject:@"Caption Test" forKey:@"InstagramCaption"];
            [self.documentInteractionController presentOptionsMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
        }
    }

问题是,当我打开弹出窗口时,我看到了其他服务,例如 facebook/twitter/mail/print/assign to contact/copy/etc。如何使我只有一个选项,即在 Instagram 中打开?

4

1 回答 1

2

Instagram 会同时收听 .jpg/.ig 文件以及名为.igo(仅限 Instagram)的特殊扩展名。通过将文件名更改为 .igo,列表中应仅显示 instagram:

NSString* tmpFileName = @"jumpto.igo";

资料来源:Instagram iPhone 挂钩

编辑:我认为您编写文件的方式存在逻辑问题。而不是使用 NSFileManager 写出这一行的内容:

[[NSFileManager defaultManager] createFileAtPath:docPath contents:imgData attributes:nil];

尝试使用 NSData 对象本身将其写入到位:

[imgData writeToFile:docPath atomically:NO];

我不确定这会导致不同的文件,但我想知道NSFileManager使用数据创建文件的方式是否与NSData直接写入数据的方式不同。

于 2013-02-10T17:52:07.430 回答