2013-05-14 21 views
2

上傳圖像我寫的代碼上傳圖像上的Instagram是如下:iPhone:在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的」選項actionsheet但是當我在,沒有什麼單擊發生。 。能請你幫我

+0

我想上傳instagram上的圖像 – user1954352 2013-05-14 08:04:11

回答

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的d以下是代表方法

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

} 
+0

感謝您的回覆。但是我的代碼得到了同樣的結果。 – user1954352 2013-05-14 08:18:31

+0

圖片尺寸應該大於620 * 620 – 2013-05-14 08:19:50

+0

並且應該在設備中安裝ya check intstagram應用程序。這是必要的 – 2013-05-14 08:20:46