通过SLComposeView将UIImagePicker的照片分享到Facebook

时间:2013-05-23 11:50:37

标签: ios facebook twitter ios6

我试图让用户从他们的相机胶卷中挑选媒体,然后使用iOS 6社交框架将其分享到Facebook。

以下是我的didFinishPickingMediaWithInfo:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    image1 = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *chosenImage =UIImageJPEGRepresentation(image1, 1.0);
    imageView.image = [UIImage imageWithData:chosenImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}

当用户点击按钮时,这就是我所说的:

-(IBAction)shareImageToFacebook:(id)sender
{
    //This is to display the UIImagePicker to let the user pick an image
    controller = [[UIImagePickerController alloc]init];
    controller.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    controller.allowsEditing=YES;
    [controller setDelegate:self];
    [self presentViewController:controller animated:YES completion:nil];

    //This is to display the SLComposeView to let the user share the photo
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbSheet setInitialText:@"Facebooking from my own app! :)"];

        [fbSheet addImage:imageView.image]; //This is where I try to add my image

        [self presentViewController:fbSheet animated:YES completion:nil];
    }
    else if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        [self dismissViewControllerAnimated:NO completion:nil];
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't post to Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

我最初尝试做的是首先让用户从相机胶卷中选择照片,然后显示Facebook分享按钮,以允许用户分享他们刚刚从相机胶卷中选取的照片。

目前,我的解决方法(在上面的代码中)是将用户选择的图像设置到不可见的UIImageView上,然后将UIImageView的图像添加到SLComposeViewController。 “解决方法”不起作用,不仅如此,控制台说我试图呈现2个视图(UIImagePickerController和SLComposeViewController)。 来自控制台的直接警告消息是:

Warning: Attempt to present <SLFacebookComposeViewController: 0x94f2580> on <UINavigationController: 0xa4b5240> while a presentation is in progress!

我想知道如何让用户在相机胶卷中选择图像,然后通过社交框架分享图像。

1 个答案:

答案 0 :(得分:0)

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

中编写faceBook代码
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    image1 = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *chosenImage =UIImageJPEGRepresentation(image1, 1.0);
    imageView.image = [UIImage imageWithData:chosenImage];
        [self dismissViewControllerAnimated:YES completion:^(void){
    //write code here



   if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbSheet setInitialText:@"Facebooking from my own app! :)"];

        [fbSheet addImage:imageView.image]; //This is where I try to add my image

        [self presentViewController:fbSheet animated:YES completion:nil];
    }
    else if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        [self dismissViewControllerAnimated:NO completion:nil];
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Sorry"
                                  message:@"You can't post to Facebook right now, make sure your device has an internet connection and you have at least one Facebook account setup"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
    }];
}