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

发布于 2024-11-19 18:23:19 字数 1699 浏览 1 评论 0原文

如果我能就如何处理我在 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。

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

如果您能提供有关如何进行的任何指示,我将非常感激。

I'd be grateful for any suggestions as to how to deal with a problem I'm having posting an image to Facebook.

The following code works just as I would expect:

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

However, I really want to use an image that I capture from my camera. I've set up the code to do that earlier in my application and I've put the image on the screen. So, I tried to do this:

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

I've tested this code and newImage is a valid UIImage that I can place and manipulate just as I'd expect. However, when I try to integrate it into the Facebook dialog as follows:

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

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

I get these two ridiculous messages in the console:
-[UIImage length]: unrecognized selector sent to instance 0x1c9420
CoreAnimation: ignoring exception: -[UIImage length]: unrecognized selector sent to instance 0x1c9420

So, as a test, I thought I'd try importing an image into my project and accessing it directly. This is the exact image as I referenced online in the first example, so I know it should work. But even when I try this:

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

I get the same error messages as above.

I see in the Facebook documentation that the "picture" key is supposed to use the URL to an image. But I've seen some examples online that indicate that folks are using an UIImage instead of just an URL.

Clearly, I'm doing something wrong, but I have no idea what it might be.

I'd be very grateful for any pointers as to how to proceed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2024-11-26 18:23:19

概述该过程的总结答案...关键点是在图形调用中包含 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];
}


7) Check for id result in FBRequestDelegate method



    -(void)request:(FBRequest *)request didLoad:(id)result {
        NSLog(@"Request didLoad: %@ ", [request url ]);
        if ([result isKindOfClass:[NSArray class]]) {
            result = [result objectAtIndex:0];
        }
        if ([result isKindOfClass:[NSDictionary class]]){

        }
        if ([result isKindOfClass:[NSData class]]) {
        }
        NSLog(@"request returns %@",result);
     }

8) 不要使用测试用户帐户进行测试...目前它将对测试帐户进行身份验证并提供访问令牌,但当您尝试使用图形 api 时,它将返回意外错误。如果您在 dev.facebook.com 中使用网络版本与本机应用程序设置,则可能没问题,因为据说您可以创建测试用户并将其与应用程序关联,但我还没有尝试过。

祝你好运!

A summarized answer that outlines the process...key point is to include the access_token in the graph calls since the facebook-ios-sdk doesn't do this automatically:

1)Update to the latest facebook-ios-sdk.
2)Instantiate with app id.



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

3) Authorize the app including publish_stream



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

4) Ensure app delegate fBDidLogin captures and stores the access token & expiration in user defaults (for later optimization of login process).



    -(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) Confirm valid access token (non nil and not expired)...otherwise re authorize app per above.
6) Capture image to UIImage & call this noting the critical addition of the access_token. This will auto create an album for your app and put the image there and post it on the wall per my testing.

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


7) Check for id result in FBRequestDelegate method



    -(void)request:(FBRequest *)request didLoad:(id)result {
        NSLog(@"Request didLoad: %@ ", [request url ]);
        if ([result isKindOfClass:[NSArray class]]) {
            result = [result objectAtIndex:0];
        }
        if ([result isKindOfClass:[NSDictionary class]]){

        }
        if ([result isKindOfClass:[NSData class]]) {
        }
        NSLog(@"request returns %@",result);
     }

8) DO NOT USE a test user account for testing...currently it will auth the test account and provide an access token but when you try to use graph api it will return unexpected errors. You may be alright if you are using web version vs native app setting in dev.facebook.com since supposedly you can create test users and associate them with the app but I haven't tried that.

Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文