我分三步完成了这个
1 张照片到相册(返回 imageID)
2 使用 imageID 请求 imageID 的元数据
3 使用“链接”字段(而不是“来源”字段)作为指向用户墙上帖子中图片的链接
缺点是现在墙上有两个帖子,一个是图片,一个是实际帖子。我还没有弄清楚如何将图片发布到相册,而不会出现墙贴(理想情况下它只是出现的第二个帖子)
第 1 步:
- (void) postImageToFacebook {
appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];
currentAPICall = kAPIGraphUserPhotosPost;
UIImage *imgSource = {insert your image here};
NSString *strMessage = @"This is the photo caption";
NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
imgSource,@"source",
strMessage,@"message",
nil];
[appDelegate.facebook requestWithGraphPath:@"me/photos"
andParams:photosParams
andHttpMethod:@"POST"
andDelegate:self];
// after image is posted, get URL for image and then start feed dialog
// this is done from FBRequestDelegate method
}
第 2 步(kAPIGraphUserPhotosPost)和第 3 步(kAPIGraphPhotoData):
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
result = [result objectAtIndex:0];
}
switch (currentAPICall) {
case kAPIGraphPhotoData: // step 3
{
// Facebook doesn't allow linking to images on fbcdn.net. So for now use default thumb stored on Picasa
NSString *thumbURL = kDefaultThumbURL;
NSString *imageLink = [NSString stringWithFormat:[result objectForKey:@"link"]];
currentAPICall = kDialogFeedUser;
appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary* dialogParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId, @"app_id",
imageLink, @"link",
thumbURL, @"picture",
@"Just Played Wizard etc etc", @"name",
nil];
[appDelegate.facebook dialog:@"feed"
andParams:dialogParams
andDelegate:self];
break;
}
case kAPIGraphUserPhotosPost: // step 2
{
NSString *imageID = [NSString stringWithFormat:[result objectForKey:@"id"]];
NSLog(@"id of uploaded screen image %@",imageID);
currentAPICall = kAPIGraphPhotoData;
appDelegate = (Scorecard4AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook requestWithGraphPath:imageID
andDelegate:self];
break;
}
}
}
我已修改代码以仅显示精简的 Facebook 内容。如果你想检查帖子是否成功,你会想要这样的:
- (void)dialogDidComplete:(FBDialog *)dialog {
switch (currentAPICall) {
case kDialogFeedUser:
{
NSLog(@"Feed published successfully.");
break;
}
}
}
Facebook 帖子中的蓝色文本是您在第 3 步中输入“名称”参数的任何内容。单击 Facebook 中的蓝色文本将带您到第 1 步中发布的照片,在 Facebook 的相册中(Facebook 创建如果您未指定相册,则为您的应用程序的默认相册)。在我的应用程序中,它是记分卡的全尺寸图像(但可以是任何图像,例如来自相机的图像)。不幸的是,我想不出一种方法来使拇指图像成为实时链接,但它的可读性不是很好,因此默认拇指适用于我的情况。 Facebook 帖子(黑色字体)中写着“年度第一场比赛...”的部分由用户输入。