如何在Iphone sdk中的facebook集成中发布带有文本的图像

时间:2010-03-23 08:19:54

标签: iphone objective-c facebook

这里我有一个问题。实际上我在我的应用程序中实现了facebook集成,我需要发布带有文本的图像,但我不知道如何处理这个。任何人都建议使用示例代码,以便它对我很有帮助。

任何人的帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

我假设你想在图像中绘制一些文字,然后将图像上传到Facebook。

首先,我们需要将原始图像和所需文本绘制成新图像。

UIGraphicsBeginImageContext(CGSizeMake(320.0, 320.0));
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw the original image
[image drawInRect:CGRectMake(0, 0, 320.0, 320.0)];
// Draw the text
[@"text" drawInRect:CGRectMake(...) withFont:[UIFont systemFontOfSize:20.0];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

然后,将图像转换为NSData并调用Facebook的“photos.upload”API上传它。

NSMutableDictionary *args = [[[NSMutableDictionary alloc] init] autorelease];
[args setObject:@"caption" forKey:@"caption"];      
FBRequest *uploadPhotoRequest = [FBRequest requestWithDelegate:self];
NSData *data = UIImagePNGRepresentation(newImage);
[uploadPhotoRequest call:@"photos.upload" params:args dataParam:data];

答案 1 :(得分:1)

如果您想将图片上传到服务器,并将一个小故事发布到Facebook的墙上。使用流API。

FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.delegate = self;
dialog.userMessagePrompt = @"Prompt";

NSString *name = @"Your caption";
NSString *src = @"http://example.com/path/of/your/image";
NSString *href = @"http://what/happens/if/the/user/click/on/the/image";

NSString *attachment = [NSString stringWithFormat:@"{\"name\":\"%@\",\"media\":[{\"type\":\"image\", \"src\":\"%@\", \"href\":\"%@\"}]}", name, src, href];
dialog.attachment = attachment;
[dialog show];  

答案 2 :(得分:0)

也许你会很高兴使用BMSocialShare。这是我写的一个简单的lib。

BMFacebookPost *post = [[BMFacebookPost alloc] 
                        initWithTitle:@"Simple sharing via Facebook, Email and Twitter for iOS!" 
                        descriptionText:@"Posting to Facebook, Twitter and Email made dead simple on iOS. Simply include BMSocialShare as a framework and you are ready to go." 
                        andHref:@"https://github.com/blockhaus/BMSocialShare"];    

[post setImageUrl:@"http://www.blockhausmedien.at/images/logo-new.gif" 
         withHref:@"http://www.blockhaus-media.com"];

[[BMSocialShare sharedInstance] facebookPublish:post];