Search code examples
objective-cfacebookfacebook-graph-apifacebook-ios-sdk

How do you get a Facebook photo's source URL using the facebook graph api?


Could you tell me how to use graph api to get the source url for a photo? Because I have the photo id and it seems like I should be able to make a call to get the source url.

As an example of what I'm trying to do, the following code works:

-(IBAction)showPicture:(UIButton *)sender;
{
    //code to retrieve picture

    id path = @"http://photos-e.ak.fbcdn.net/photos-ak-snc1/v5041/89/51/40796308305/a40796308305_1960517_6704612.jpg";
    NSURL *url = [NSURL URLWithString:path];
    NSData  *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
    [self.label setText: (NSString *)path];
    imageView.image = img;
    [img release];

}

However that works when I have the source url. Is there a way to use graph api so that I can substitute id path = @"http://..." for something like id path = [_facebook requestWithGraphPath:@"98423808305/source" andDelegate:self]; ???


Updated to include request didLoad method

- (void)request:(FBRequest *)request didLoad:(id)result {


    if ([request.params objectForKey:@"picture"]) {
        // Get photo ID from result
        NSString *photoId = (NSString *)[result objectForKey:@"id"];
        shitStorm = photoId;
    }


  if ([result isKindOfClass:[NSArray class]]) {
    result = [result objectAtIndex:0];
  }
  if ([result objectForKey:@"owner"]) {
    [self.label setText:@"Photo upload Success"];
  } else {
    [self.label setText:[result objectForKey:@"name"]];
  }
};

Solution

  • [_facebook requestWithGraphPath:@"YOUR_PHOTO_ID" andDelegate:self];  
    

    then in the request response you'll get every information about the picture. See an example here:
    https://graph.facebook.com/98423808305
    Grab your required info, might against the key "source", and use it as you want. http://developers.facebook.com/docs/reference/api/ this is a very good place to learn while developing with FB.

    - (void)request:(FBRequest*)request didLoad:(id)result {  
        NSLog(@"%@",result);
        NSArray *keys=[result allKeys];
        NSLog(@"%@",keys);
        NSString *imageURL=[result objectForKey:@"source"];
        //here you can use this imageURL to get image-data and display it in imageView  
    }  
    

    Might this will give you idea how to sort different requests.

    - (void)request:(FBRequest*)request didLoad:(id)result{
        NSLog(@"%@",result);
        NSString *requestType =[request.url stringByReplacingOccurrencesOfString:@"https://graph.facebook.com/" withString:@""];
        if ([requestType isEqualToString:@"me"]) {
            //Request to get User's Profile info
        }
        else if ([requestType isEqualToString:@"me/picture"]) {
            //this is the request to get User's Profile Picture
        }   
    }  
    

    Similarly you can sort different requests and hence perform different request specific tasks.