如何在iOS上将照片分享到Facebook



我试图在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];
}

if ([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

要在脸书上发布的图片网址。

NSData* data = [NSData dataWithContentOfURL:[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];
        }

试试这个

if ([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

要在脸书上发布的图片网址。

NSData* data = [NSData dataWithContentOfURL:[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];
        }

最新更新