2011-07-07 192 views
0

你能告诉我如何使用图形API来获取照片的源网址吗?因为我有照片ID,所以我应该可以打电话来获取源网址。如何使用脸谱图API获取Facebook照片的源URL?

由于什么,我试图做一个例子,下面的代码工作:

-(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]; 

} 

然而,当我源URL的作品。有没有办法使用图形API,以便我可以用id path = @"http://..."代替id path = [_facebook requestWithGraphPath:@"98423808305/source" andDelegate:self];这样的东西?


更新以包括请求didLoad方法

- (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"]]; 
    } 
}; 

回答

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

然后在请求响应,你会得到有关图片的每个信息。这里看一个例子:
https://graph.facebook.com/98423808305
抓住你需要的信息,可能对键“来源”,并根据需要使用它。 http://developers.facebook.com/docs/reference/api/这是一个用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 
} 

可能这会给你想法如何排序不同的请求。

- (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 
    } 
} 

同样,您可以对不同的请求进行排序,从而执行不同的请求特定任务。

+0

嗨Admed,感谢您的回复 - 我们都使用相同的FB例子。不过我理解请求响应的样子。我不明白如何获取所需的信息。因此,使用你的榜样,我会做这样的事情'ID路径= [_facebook requestWithGraphPath:@ “YOUR_PHOTO_ID” andDelegate:自我 “];其次NSURL * URL = [NSURL URLWithString:[(的NSString *)[路径objectForKey:@” 源“]]]?'...这是你告诉我做 – NateHill

+0

没有NateHill你需要实现fbRequestDelegateProtocol ..在它的requestdidload方法,我想,你会得到所需要的数据的响应很可能是字典格式,你应该NSLOG所有你收到的答复,以获得更好的理解,告诉我,如果有帮助,或者我会尝试把一些示例代码。 – Ahmed

+0

这样的帮助。我包含了我的请求didLoad方法,所以如果你可以使用它作为你的例子,那将会很棒,我猜我对“代码”应该在哪里感到困惑,在我的showPicture方法中,我只是调用'[_facebook requestWithGraphPath:@ “YOUR_PHOTO_ID”和Delegate:self“]',然后在我的请求中didLoad方法我把实际的代码加载UIImage视图?如果是这样,该方法如何知道什么时候该做什么? – NateHill

相关问题