简体   繁体   中英

Sharing facebook dialog by using UIImage as @“picture”

Here is simple code

UIImage *ScreenShot = [self getScreenshot];

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Name", @"name",
                               @"Caption.", @"caption",
                               @"Description.", @"description",
                               @"www.example.com", @"link",
                               ScreenShot, @"picture",               
                               nil];

[ facebook dialog:@"feed"
                  andParams:params
                andDelegate:self];

And when I compile it get this message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x1288bb90' *** First throw call stack:

You can't directly upload an image to the wall, when publishing to the wall you can just link an image to it. So, you need to upload the UIImage somewhere first. You have 2 options:

  1. Upload to your/some server and publish a wall post linking to it.
  2. Upload to a facebook album. Check the graph API about doing that, it is pretty straightforward

I just had a look at the Facebook API in order to see what you are doing here. To my mind, this

http://www.facebook.com/dialog/feed?
  app_id=123050457758183&
  link=http://developers.facebook.com/docs/reference/dialogs/&
  picture=http://fbrell.com/f8.jpg&
  name=Facebook%20Dialogs&
  caption=Reference%20Documentation&
  description=Using%20Dialogs%20to%20interact%20with%20users.&
  redirect_uri=http://www.example.com/response

clearly says what Facebook wants you to do. Do you see that the picture parameter is not an image itself but a link to an image? You'll have to upload the image first and then put the Link to the image into this parameter.

I'm not sure if this is the only error in your code but it's a major one for sure.

Import an image into your project and try to change the line UIImage *ScreenShot = [self getScreenshot] to : UIImage *ScreenShot = [UIImage imageNamed:@"yourimage.png"];

If it works, you may probably have problem in your "getScreenshot" method.

I use this for my app :

    UIImage *ImageView = [UIImage imageNamed:@"anImage.png"];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       ImageView, @"picture",@"Name", @"name",
                                       nil];

         //Let's post it
         [facebook requestWithGraphPath:@"me/photos"
         andParams:params
         andHttpMethod:@"POST"
         andDelegate:self];


-(void)request:(FBRequest *)request didLoad:(id)result{
    if ([result objectForKey:@"id"]) {


        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"MyApp" message:@"Your image has been posted on your wall!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [al show];
        [al release];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM