facebook ios sdk - 使用 UIImage 发布照片时遇到问题
如果我能就如何处理我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
概述该过程的总结答案...关键点是在图形调用中包含 access_token,因为 facebook-ios-sdk 不会自动执行此操作:
1)更新到最新的 facebook-ios-sdk。
2)用app id实例化。
3) 授权应用程序,包括publish_stream
4) 确保应用程序委托 fBDidLogin 捕获并存储访问令牌和访问令牌。用户默认过期(用于以后优化登录过程)。
5) 确认有效的访问令牌(非零且未过期)...否则按照上述重新授权应用程序。
6) 捕获图像到UIImage &调用此方法时请注意 access_token 的关键添加。这将为您的应用程序自动创建一个相册,并将图像放在那里,并根据我的测试将其张贴在墙上。
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.
3) Authorize the app including publish_stream
4) Ensure app delegate fBDidLogin captures and stores the access token & expiration in user defaults (for later optimization of login process).
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.