facebook ios sdk - 使用UIImage发布照片的麻烦 [英] facebook ios sdk - trouble posting photo using UIImage

查看:158
本文介绍了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 length]:无法识别选择器发送到实例0x1c9420
CoreAnimation:忽略异常: - [UIImage length]:发送到实例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)授权包括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天全站免登陆