2013-01-19 1 views
0

에 사진을 내보내기 :이 코드 사용, 인스 타 그램에 내 응용 프로그램에서 사진을 수출하려고 인스 타 그램

// Capture Screenshot 
UIGraphicsBeginImageContextWithOptions(self.view.frame.size,self.view.opaque,0.0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"]; 
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]]; 
dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]]; 
dic.UTI = @"com.instagram.photo"; 
dic.annotation = [NSDictionary dictionaryWithObject:@"my caption" forKey:@"InstagramCaption"]; 
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; 
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { 
    [[UIApplication sharedApplication] openURL:instagramURL]; 
} else { 
    NSLog(@"No Instagram Found"); 
} 

을하지만 그것은 작동하지 않습니다. 그것은 다음과 같은 오류와 충돌 :

2013-01-19 20:40:41.948 Ayat[12648:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

내가 제대로 아니에요 확신 :

1 내가 찍은 스크린 샷으로 jpgpath 저장.

2 "dic"문서를 Instagram으로 전달.

+0

"작동하지 않음"을 정의하십시오. 컴파일러 경고/오류? 추락? 뭐? 구체적으로 말하십시오. 디버거를 사용 했습니까? 모든 변수에 예상 값이 있는지 확인 했습니까? 'if' 문은 어떻게됩니까? – rmaddy

+0

maddy .. 다음 오류로 인해 충돌이 발생합니다. 2013-01-19 20 : 40 : 41.948 Ayat [12648 : c07] *** 캐치되지 않은 예외로 인해 앱이 종료 됨 'NSInvalidArgumentException', 이유 : '*** - [ NSWRL initFileURLWithPath :] : nil 문자열 매개 변수 ' 잘 모르겠다. 1- jpgpath를 찍은 스크린 샷으로 저장 "dic"문서가있는 2 단계 인스 타 그램 – user1767928

+0

추가 세부 사항으로 질문을 업데이트하십시오. – rmaddy

답변

12

내 앱에서 Instagram에 사진을 내보냈습니다.

-(void)shareImageOnInstagram:(UIImage*)shareImage 
{ 
    //Remember Image must be larger than 612x612 size if not resize it. 

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; 

    if([[UIApplication sharedApplication] canOpenURL:instagramURL]) 
    { 
     NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
     NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.ig"]; 
     NSData *imageData=UIImagePNGRepresentation(shareImage); 
     [imageData writeToFile:saveImagePath atomically:YES]; 

     NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath]; 

     UIDocumentInteractionController *docController=[[UIDocumentInteractionController alloc]init]; 
     docController.delegate=self; 
     [docController retain]; 
     [email protected]"com.instagram.photo"; 

     docController.annotation=[NSDictionary dictionaryWithObjectsAndKeys:@"Image Taken via @App",@"InstagramCaption", nil]; 

     [docController setURL:imageURL]; 


     [docController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES]; //Here try which one is suitable for u to present the doc Controller. if crash occurs 

    } 
    else 
    { 
     NSLog (@"Instagram not found"); 

    } 

} 

나는이 뜻은 당신이 도움이되기를 바랍니다 : 이 코드를 사용해보십시오 (이미지는 612x612 크기보다 커야합니다). 당신이 fileURLWithPath:를 호출이 줄

dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:igImageHookFile]]; 

+0

고마워. 내 질문에 답변하고 완벽하게 작업했습니다 – user1767928

+0

iPad에서 정상적으로 작동합니까? – user1767928

+0

예, iPad에서도 작동합니다. –

2

. 처음에는 NSURL이 아닌 NSString이 필요하지만 오류 메시지에서 igImageHookFilenil 인 것 같습니다.

0

많은 연구가 필요했지만 결국이 사이트에서 도움이되는 것으로 나타났습니다. 당신은 단지 그림에서 퍼팅하는 경우 난 그냥 그 자신의 원래의 문제였다으로 캡션을 포함하지 않도록 조금 그것을 손질 ...하지만, 여기있다 :

-(void)shareImageOnInstagram 
{ 
//Remember Image must be larger than 612x612 size if not resize it. 
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"]; 
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]){ 
    NSString *urlString = [[NSString alloc] initWithFormat:@"file://%@", blendedImageURL]; 
    NSURL *imageUrl = [[NSURL alloc] initWithString: urlString]; 
    self.docController = [self setupControllerWithURL:imageUrl usingDelegate:self]; 
    self.docController.UTI = @"com.instagram.exclusivegram"; 
    // self.docController.annotation = [NSDictionary dictionaryWithObject:@"I want to  share this text" forKey:@"InstagramCaption"]; 
    [self.docController presentOpenInMenuFromRect: self.view.frame inView: self.view  animated: YES ];} 
} 
0

나는 다음 코드를 시도하고 근무 :

- (void) shareImageWithInstagram 
{ 
    NSURL *instagramURL = [NSURL URLWithString:@"instagram://"]; 
    if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) 
    { 
     UICachedFileMgr* mgr = _gCachedManger; 
     UIImage* photoImage = [mgr imageWithUrl:_imageView.image]; 
     NSData* imageData = UIImagePNGRepresentation(photoImage); 
     NSString* captionString = [NSString stringWithFormat:@"%@%@",self.approvedData.approvedCaption,self.approvedData.tag]; 
     NSString* imagePath = [UIUtils documentDirectoryWithSubpath:@"image.igo"]; 
     [imageData writeToFile:imagePath atomically:NO]; 
     NSURL* fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"file://%@",imagePath]]; 

     self.docFile = [[self setupControllerWithURL:fileURL usingDelegate:self]retain]; 
     self.docFile.annotation = [NSDictionary dictionaryWithObject: captionString 
                 forKey:@"InstagramCaption"]; 
     self.docFile.UTI = @"com.instagram.photo"; 

     // OPEN THE HOOK 
     [self.docFile presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES]; 
    } 
    else 
    { 
     [UIUtils messageAlert:@"Instagram not installed in this device!\nTo share image please install instagram." title:nil delegate:nil]; 
    } 
} 
관련 문제