0

我将如何将文件中的图像发布到 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];
4

2 回答 2

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];
    }
}
于 2013-05-14T10:01:37.003 回答
1

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

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

然后在将 fbImage 添加到 SLComposeViewController 类的实例之前检查它是否不是 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];

}

于 2013-05-14T10:09:40.927 回答