0

我试图让用户从他们的相机胶卷中挑选媒体,然后使用 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!

我想知道如何让用户在他们的相机胶卷中选择一张图片,然后通过社交框架分享这张图片。

4

1 回答 1

0

将facebook代码写入-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

-(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];
    }
    }];
}
于 2013-05-23T11:54:18.680 回答