facebook ios sdk - 使用 UIImage 发布照片时遇到问题

2023-11-24

如果我能就如何处理我在 Facebook 上发布图片时遇到的问题提供任何建议,我将不胜感激。

下面的代码正如我所期望的那样工作:

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                 @"http://myserver/img.png", @"picture",
                 @"My Testing", @"name",
                 @"Enter some text...",  @"message",
                 nil];

[appDelegate.facebook dialog:@"feed" andParams:params andDelegate:self];

然而,我真的很想使用我的相机捕捉到的图像。我已经在应用程序的早期设置了代码来执行此操作,并将图像放在屏幕上。所以,我尝试这样做:

CGRect contextRect  = CGRectMake(0, 0, 320, 436);
UIGraphicsBeginImageContextWithOptions(contextRect.size, YES, 1);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我已经测试了这段代码,并且 newImage 是一个有效的 UIImage,我可以按照我的预期放置和操作它。但是,当我尝试将其集成到 Facebook 对话框中时,如下所示:

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                 newImage, @"picture",
                 @"My Testing", @"name",
                 @"Enter some text...",  @"message",
                 nil];

[appDelegate.facebook dialog:@"feed" andParams:params andDelegate:self];

我在控制台中收到两条荒谬的消息: -[UIImage length]:无法识别的选择器发送到实例 0x1c9420 CoreAnimation:忽略异常:-[UIImage length]:无法识别的选择器发送到实例 0x1c9420

因此,作为测试,我想尝试将图像导入到我的项目中并直接访问它。这与我在第一个示例中在线引用的图像完全相同,所以我知道它应该可以工作。但即使当我尝试这样做时:

UIImage *newImage = [UIImage imageNamed:@"BaB.png"];

我收到与上面相同的错误消息。

我在 Facebook 文档中看到“图片”键应该使用图像的 URL。但我在网上看到一些示例,表明人们正在使用 UIImage 而不仅仅是 URL。

显然,我做错了什么,但我不知道它可能是什么。

我将非常感谢任何有关如何继续的指示。


概述该过程的总结答案...关键点是将 access_token 包含在图形调用中,因为 facebook-ios-sdk 不会自动执行此操作:

1)更新到最新的facebook-ios-sdk。
2)用app id实例化。



      self.facebook = [[Facebook alloc] initWithAppId:kApplicationFBID]; 

3)授权应用程序,包括publish_stream



  NSArray * neededPermissions = [[[NSArray alloc] initWithObjects:@"user_about_me", @"publish_stream", @"user_photos", nil] autorelease];
  [facebook authorize:neededPermissions delegate:appDelegate];

4) 确保应用程序委托 fBDidLogin 捕获访问令牌和过期时间并将其存储在用户默认值中(用于以后优化登录过程)。



    -(void)fbDidLogin {
        DebugLog(@"New Access Token: %@", [facebook accessToken] );
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
        [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }

5) 确认有效的访问令牌(非零且未过期)...否则按照上述重新授权应用程序。
6) 将图像捕获到 UIImage 并调用此方法,注意 access_token 的关键添加。这将为您的应用程序自动创建一个相册,并将图像放在那里,并根据我的测试将其张贴在墙上。

-(void)postImageToAlbum:(UIImage *)image {
  NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
    image, @"source", 
    @"caption desc", @"message",             
    nil];
  [facebook requestWithGraphPath:[NSString stringWithFormat:@"/me/photos?access_token=%@", self.facebook.accessToken]
  andParams:params andHttpMethod:@"POST" andDelegate:self];
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

facebook ios sdk - 使用 UIImage 发布照片时遇到问题 的相关文章

随机推荐