iPhone:在Instagram上上传图片

时间:2013-05-14 07:58:20

标签: iphone ios6 instagram uidocument

我已经编写了在instagram上传图片的代码,如下所示:

CGRect rect = CGRectMake(20 ,20 , 200, 200);
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *jpgPath = [documentsDirectory stringByAppendingPathComponent:@"after2sm.ig"];

    NSString *fileURL = [NSString stringWithFormat:@"file://%@",jpgPath];
    NSURL *igImageHookFile = [NSURL fileURLWithPath:fileURL];

    self.dic.UTI = @"com.instagram.photo";

    self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
    self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];

    [NSDictionary dictionaryWithObject:@"My Caption" forKey:@"InstagramCaption"];
    [self.dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];

但是它会打开一个带有“instagram”选项的动作表,但是当我点击它没有任何反应时......请你帮帮我

1 个答案:

答案 0 :(得分:6)

试试这个, 在此代码中,您可以在imgUpload对象中设置图像

    - (IBAction)InstagramButtonClick
   { 

        CGRect rect = CGRectMake(0.0, 0.0, 612, 612);
        UIGraphicsBeginImageContext(rect.size);
        [imgUpload drawInRect:rect];

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        NSString  *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];
        [UIImageJPEGRepresentation(img, 1.0) writeToFile:savePath atomically:YES];

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


        self.dic.UTI = @"com.instagram.photo";
        self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];

        self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
          self.dic.annotation = [NSDictionary dictionaryWithObject:@"your message" forKey:@"InstagramCaption"];
        [self.dic presentOpenInMenuFromRect: CGRectZero    inView: self.view animated: YES ];

    }

//添加委托UIDocumentInteractionControllerDelegate,以下是委托方法

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
        UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
        interactionController.delegate = interactionDelegate;
        return interactionController;

}