Search code examples
iosfacebook-graph-apihttp-post

How to post image to Facebook wall with graph API


I want to post some text with image on Facebook wall. I have this code from graph API which does that but I want to post my image from bundle and not from a link as this API does. Can anyone tell me how to post your own image rather then the link

"[variables setObject:@"http://bit . ly/bFTnqd" forKey:@"link"];" 

on Facebook wall.

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];

[variables setObject:@"this is a test message: postMeFeedButtonPressed" forKey:@"message"];

UIImage *picture = [UIImage imageNamed:@"forest fog.jpg"];
        
[variables setObject:@"http://bit . ly/bFTnqd" forKey:@"link"];
[variables setObject:@"This is the bolded copy next to the image" forKey:@"name"];
[variables setObject:@"This is the plain text copy next to the image.  All work and no play makes Jack a dull boy." forKey:@"description"];

    

FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/feed" withPostVars:variables];
NSLog(@"postMeFeedButtonPressed:  %@", fb_graph_response.htmlResponse);

//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];   
[parser release];

//let's save the 'id' Facebook gives us so we can delete it if the user presses the 'delete /me/feed button'
self.feedPostId = (NSString *)[facebook_response objectForKey:@"id"];
NSLog(@"feedPostId, %@", feedPostId);
NSLog(@"Now log into Facebook and look at your profile...");

Solution

  • You covert Image into data and add that as another parameter (Data value for key "data") in the Dictionary.I have given sample here

    [variables setObject:imageData forKey:@"data"];
    

    Other Change

    UIImage *picture = [UIImage imageNamed:@"forest fog.jpg"];
    
    NSData *imageData = UIImageJPEGRepresentation(picture, 10);
    
    
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"your caption message",@"message", 
                                   imageData,@"data",
                                   nil];
    
    FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/photos" withPostVars:variables];
    NSLog(@"postMeFeedButtonPressed:  %@", fb_graph_response.htmlResponse);