facebook ios sdk - 使用 UIImage 发布照片时遇到问题 [英] facebook ios sdk - trouble posting photo using UIImage

查看:15
本文介绍了facebook ios sdk - 使用 UIImage 发布照片时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有任何关于如何处理我在 Facebook 上发布图片的问题的建议,我将不胜感激.

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

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

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

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

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.

我在 Facebook 文档中看到图片"键应该使用图像的 URL.但是我在网上看到了一些例子,表明人们使用的是 UIImage 而不仅仅是 URL.

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.

推荐答案

概述过程的总结答案...关键点是在图形调用中包含 access_token,因为 facebook-ios-sdk 不这样做自动:

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)更新到最新的facebook-ios-sdk.
2)用应用程序ID实例化.

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



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

3) 授权app,包括publish_stream

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) 确保应用委托 fBDidLogin 捕获并存储访问令牌和用户默认过期(用于以后登录过程的优化).

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

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) 在 FBRequestDelegate 方法中检查 id 结果

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 中使用网络版本与本机应用程序设置,您可能会没事,因为据说您可以创建测试用户并将他们与应用程序相关联,但我没有尝试过.

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.

祝你好运!

这篇关于facebook ios sdk - 使用 UIImage 发布照片时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆