2015-03-13 54 views
-1

我試圖在Facebook上分享圖片,但我不能。這是我正在使用的代碼:如何在iOS上分享照片到Facebook?

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
    SLComposeViewController * fbSheetOBJ = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 

    SLComposeViewControllerCompletionHandler myBlock = 
    ^(SLComposeViewControllerResult result){ 
     [fbSheetOBJ dismissViewControllerAnimated:YES completion:nil]; 
    }; 
    fbSheetOBJ.completionHandler = myBlock; 

    [fbSheetOBJ addURL:[NSURL URLWithString:@"http://www.test.com"]]; 
    [fbSheetOBJ setInitialText:@"Post from my iOS application"]; 
    [fbSheetOBJ addImage:[UIImage imageNamed:@"download.jpeg"]]; 

    [self presentViewController:fbSheetOBJ animated:YES completion:Nil]; 
} 
+0

你確定[UIImage imageNamed:@「download.jpeg」]在那裏。檢查是否([UIImage imageNamed:@「download.jpeg」])這個 – DilumN 2015-03-13 13:54:04

回答

0

如果([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){

SLComposeViewController * mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

  NSString *desc = @"the Text what you want to post in in facebook";     
      [mySLComposerSheet addURL:[NSURL URLWithString:@"yourLink"]]; 
      [mySLComposerSheet setInitialText:desc]; 
      //convert any type of image to png format 

//圖片URL在Facebook上發佈。我們可以通過下面的例子來介紹一下NSData的數據類型:[NSURL URLWithString:[self.imageurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

  UIImage *image = [UIImage imageWithData:data]; 
      data = UIImagePNGRepresentation(image); 
      UIImage *pngImage = [UIImage imageWithData:data]; 



      // image convertion ends.. 

      [mySLComposerSheet addImage: image]; 

      [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { 

       switch (result) { 
        case SLComposeViewControllerResultCancelled: 

        alert = [[UIAlertView alloc]initWithTitle:@"Cancelled" message:@"You Cancelled posting the Deal." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil]; 
         [alert show]; 
         [alert release]; 
         break; 
        case SLComposeViewControllerResultDone: 

         alert1 = [[UIAlertView alloc]initWithTitle:@"Success" message:@"The Deal was posted successfully." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil]; 
         [alert1 show]; 
         [alert1 release]; 
         break; 

        default: 
         break; 
       } 
      }]; 

      [self presentViewController:mySLComposerSheet animated:YES completion:nil]; 
     } 
+0

如何將圖像轉換爲網址 – 2015-03-13 14:07:53

+0

要麼共享本地圖像,要麼從互聯網 – 2015-03-16 06:37:39

+0

如果它是本地圖像,你可以轉換成NSData,然後你可以發送。 – 2015-03-16 06:45:58

0

嘗試此

如果([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){

SLComposeViewController * mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

  NSString *desc = @"the Text what you want to post in in facebook";     
      [mySLComposerSheet addURL:[NSURL URLWithString:@"yourLink"]]; 
      [mySLComposerSheet setInitialText:desc]; 
      //convert any type of image to png format 

//圖片URL在Facebook上發佈。我們可以通過下面的例子來介紹一下NSData的數據類型:[NSURL URLWithString:[self.imageurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

  UIImage *image = [UIImage imageWithData:data]; 
      data = UIImagePNGRepresentation(image); 
      UIImage *pngImage = [UIImage imageWithData:data]; 



      // image convertion ends.. 

      [mySLComposerSheet addImage: image]; 

      [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { 

       switch (result) { 
        case SLComposeViewControllerResultCancelled: 

        alert = [[UIAlertView alloc]initWithTitle:@"Cancelled" message:@"You Cancelled posting the Deal." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil]; 
         [alert show]; 
         [alert release]; 
         break; 
        case SLComposeViewControllerResultDone: 

         alert1 = [[UIAlertView alloc]initWithTitle:@"Success" message:@"The Deal was posted successfully." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil]; 
         [alert1 show]; 
         [alert1 release]; 
         break; 

        default: 
         break; 
       } 
      }]; 

      [self presentViewController:mySLComposerSheet animated:YES completion:nil]; 
     } 
相關問題