将文件中的图像发布到Facebook?

时间:2013-05-14 09:56:10

标签: iphone ios objective-c facebook

如何将文件从文件发布到Facebook?这很好用:

SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controllerSLC setInitialText:@" iPhone app"];
[controllerSLC addURL:[NSURL URLWithString:@"http://www.example.com"]];

[controllerSLC addImage:[UIImage imageNamed:@"icon.png"]];

但是我正在尝试发送图像:

 self.imageView.image = [self.photo objectForKey:photoPictureKey];

2 个答案:

答案 0 :(得分:1)

试试这个配偶(iOS6),

将您的图片发送到此方法,

确保您添加Social.framework和#import <Social/Social.h>

-(void) uploadToFaceBook:(UIImage *) image{


    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){

        SLComposeViewController *fbController =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        SLComposeViewControllerCompletionHandler __block completionHandler=
        ^(SLComposeViewControllerResult result){

            [fbController dismissViewControllerAnimated:YES completion:nil];

            switch(result){
                case SLComposeViewControllerResultCancelled:
                default:
                {
                    NSLog(@"Cancelled.....");
                }
                    break;
                case SLComposeViewControllerResultDone:
                {
                    NSLog(@"Posted....");
                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sent" message:nil delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
                    [alert show];
                }
                    break;
            }};

        [fbController addImage:image];
        [fbController setInitialText:@" iPhone App"];
        [fbController addURL:[NSURL URLWithString:@"http://www.example.com"]];

        [fbController setCompletionHandler:completionHandler];
        [self presentViewController:fbController animated:YES completion:nil];
    }
    else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry"
                                                            message:@"You can't post on Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
    }
}

答案 1 :(得分:1)

如果这个[self.photo objectForKey:photoPictureKey]代码返回图像的路径,你可以使用UIImage类方法,即imageWithContentsOfFile:

UIImage *fbImage = [UIImage imageWithContentsOfFile:[self.photo  objectForKey:photoPictureKey]];

然后在将它添加到类SLComposeViewController的实例之前检查fbImage是不是nil

 if(fbImage!=nil){
  [controllerSLC addImage: image];
 }else{
  NSLog('image is nil'); 
 }

如果这个“[self.photo objectForKey:photoPictureKey]”代码返回一个UIImage实例,那么这样做:

 UIImage *fbImage = [self.photo objectForKey:photoPictureKey];
 if(fbImage!=nil){
  [controllerSLC addImage: fbImage];
 }else{
  NSLog('image is nil'); 
 }

您还可以通过以下方式检查fbImage是否为空:

if(fbImage!=nil && !([fbImage isKindOfClass:[NSNull class]])){
    [controllerSLC addImage: fbImage];

}