简体   繁体   中英

how to share image on Facebook in iphone

I'm facing a problem in my code. I'm using this code:

//create a UIImage (you could use the picture album or camera too)

NSData *imageData = UIImageJPEGRepresentation(imgView.image, 8.0);
UIImage *picture = [UIImage imageWithData:imageData];

//create a FbGraphFile object insance and set the picture we wish to publish on it
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];

//finally, set the FbGraphFileobject onto our variables dictionary....
[variables setObject:graph_file forKey:@"file"];    
[variables setObject:@"i'm testing my iPhone App" forKey:@"message"];

//the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
//object and treat that is such.....
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"117795728310/photos" withPostVars:variables];
NSLog(@"postPictureButtonPressed:  %@", fb_graph_response.htmlResponse);    
NSLog(@"Now log into Facebook and look at your profile & photo albums...");

In this code, I get the EXC_BAD_ACCESS error message, but the image is posted on Facebook.

How can I solve this problem?

-(void)postInAppFacebook
{
    NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:3];
    UIImage *picture = FilterImage;
    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];
    [variables setObject:graph_file forKey:@"file"];
    [variables setObject:[NSString stringWithFormat:@"Hello testin"] forKey:@"message"];
    [fbGraph doGraphPost:@"me/photos" withPostVars:variables];
    NSLog(@"Now log into Facebook and look at your profile & photo albums...");

}

Finally i found the solution for upload images on facebook from. Use below code. It will work sure.

- (void)uploadPhoto{
    NSMutableDictionary *args = [[NSMutableDictionary alloc] init];
    UIImage *img = [UIImage imageNamed:@"picture1.jpg"];
    NSData *data = UIImageJPEGRepresentation(img, 10);
    //NSData *data = UIImagePNGRepresentation(img);
    NSString *imgstr = [data base64Encoding];
    //NSString *imgstr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    //[args setObject:[UIImage imageNamed:@"antartica1.png"] forKey:@"image"];   
    [args setObject:imgstr forKey:@"image"];    // 'images' is an array of 'UIImage' objects
    uploadimgrequest = [FBRequest requestWithDelegate:self];
    [uploadimgrequest call:@"photos.upload" params:args dataParam:data];
    //[uploadimgrequest call:@"photos.upload" params:args];
}

get this code !!!!

or

to post a image use the following code (extracted from demo app

- (IBAction) publishStream: (id)sender {

  SBJSON *jsonWriter = [[SBJSON new] autorelease];

  NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys: 
                               @"Always Running",@"text",@"http://itsti.me/",@"href", nil], nil];

  NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];

  NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"a long run", @"name",
                            @"The Facebook Running app", @"caption",
                            @"it is fun", @"description",
                            @"http://itsti.me/", @"href",
                            [NSArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"image", @"type",
                              @"http://www.gogle.com/", @"href",
                              @"http://a1.twimg.com/profile_images/457512510/iconoTwitter_bigger.gif", @"src",nil]
                             , nil ]
                            ,@"media", nil];


  NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 kAppId, @"api_key",
                                 @"Share on Facebook",  @"user_message_prompt",
                                 actionLinksStr, @"action_links",
                                 attachmentStr, @"attachment",
                                 nil];


  [_facebook dialog: @"stream.publish"
          andParams: params
        andDelegate:self];

}

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