分享照片到Instagram的iphone应用程序

时间:2014-05-23 22:19:24

标签: ios objective-c instagram

我使用以下代码示例将Instagram照片共享添加到我的iphone应用程序中:

http://sourceforge.net/projects/nimitparekh/files/Open%20instagram/OpenInstagramController.zip/download

我在代码示例和我的应用程序中遇到了同样的问题。 "打开"菜单出现在Instagram,iPhoto等中,但推送时没有任何应用程序打开。你们其中一个天才能看一看代码示例并告诉我我错过了什么吗?谢谢!

这是.h代码:

@interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>{
    IBOutlet UIImageView *imageView;
}
@property(nonatomic,retain)UIDocumentInteractionController *docFile;
@property (nonatomic, retain) UIDocumentInteractionController *dic;
-(IBAction)saveToInstagram:(id)sender;

这里是.m代码:

@synthesize docFile = _docFile;

-(IBAction)saveToInstagram:(id)sender {
        CGRect rect = CGRectMake(0 ,0 , 0, 0);

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.ig"];

    NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];

    self.docFile.UTI = @"com.instagram.photo";
    self.docFile = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
    self.docFile=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
    [self.docFile presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];
    NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"];
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
           [self.docFile presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];
    }
    else {   
        NSLog(@"No Instagram Found");
    }

}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

感谢。

1 个答案:

答案 0 :(得分:0)

我不确定您如何构建文件NSURL实例,但是从代码中可以清楚地看出,您实际上并未保存所呈现的图像。 在致电UIGraphicsEndImageContext()之前,您需要执行类似

的操作
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

然后像

    NSData * imageData = UIImageJPEGRepresentation(image, 0.75);
    [imageData writeToURL: igImageHookFile atomically:YES];