如何通过 iphone 应用程序将照片/图像上传到 facebook
Posted
技术标签:
【中文标题】如何通过 iphone 应用程序将照片/图像上传到 facebook【英文标题】:how to uload photo/image to facebook via iphone application 【发布时间】:2010-06-02 12:03:07 【问题描述】:我正在将 facebook 与我的 iphone 应用程序集成。我可以分享 脸书上的短信,但我想分享/上传 通过我的 iphone 应用程序将照片上传到 facebook。 请让我知道是否有解决方案。 我正在尝试 facebook 示例代码,但它给了我一些 错误:我发现了那个错误,但其他开发人员也 面对这种类型的错误。请让我知道是否有 有人在 iphone 的 facebook 上上传/分享照片 应用。
提前致谢。
【问题讨论】:
Documented process for using facebook connect for the iPhone to upload photos的可能重复 【参考方案1】:你需要创建一个相册来上传图片,在你通过facebook api创建一个相册后,你必须使用它的id来上传照片
// for making a facebook album u have to make a multi-part form request
//to this url https://graph.facebook.com/100002232753228/albums
//where 100002232753228 is the fbid of the user and with form fields access_token,name,message, for reference go to -http://developers.facebook.com/docs/reference/api/album/
//To upload a foto to album send multi-part data to the url - https://graph.facebook.com/102657189818706/photos
//where 102657189818706 is the facebook id of the album created
这里是代码
#define kRequestBoundary @"----FOO"
NSMutableData *postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", kRequestBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; //starting the boundary of form data
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; //setting field name of access_toke
[postBody appendData:[[NSString stringWithString:@"200726069952695|fd9c7d8ed4b5592b1240cdb3-100002232753228|i7ae5By6VFItLliVT1jw5jsETEY"] dataUsingEncoding:NSUTF8StringEncoding]];
//setting field value of access_token
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kRequestBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; //closing the boundary for next field
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"UserImage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//setting the field name of image
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; //setting the content-Type of image
UIImage *cropped=[UIImage imageNamed:@"Sample.png"];
NSData *pImageData = UIImagePNGRepresentation(cropped);
[postBody appendData:pImageData]; // adding image data to the request data
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",kRequestBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; //closing the
NSString *urlString = @"https://graph.facebook.com/102657189818706/photos";
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:pUrl]];
self.mURLrequest=request;
[request release];
[pUrl release];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", kRequestBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
[mURLrequest setHTTPBody:postBody];
NSURLConnection *temp=[[NSURLConnection alloc] initWithRequest:mURLrequest delegate:self];
[temp autorelease];
【讨论】:
以上是关于如何通过 iphone 应用程序将照片/图像上传到 facebook的主要内容,如果未能解决你的问题,请参考以下文章
我们如何将图像或照片上传到 ejabberd XMPP Web 服务器?
目标 C - 将图像从服务器下载到 iPhone 并保留日期