Ios 使用SLRequest在tweet中发布图像-图像未显示
我正在尝试使用SLRequest以编程方式将图像(从iOS设备)上载到用户的twitter帐户。推文文本上传到推文中,但图像从未出现。响应状态代码为200 显然,基本功能正常,否则tweet将根本不会显示在用户流中,我已经检查了图像是否找到,以及(NSData)变量是否包含预期的字节数 查看下面的代码-正如您从注释代码中看到的,我尝试了各种不同的修改,但没有成功。。。感谢您的任何想法/帮助Ios 使用SLRequest在tweet中发布图像-图像未显示,ios,twitter,uiimage,slrequest,Ios,Twitter,Uiimage,Slrequest,我正在尝试使用SLRequest以编程方式将图像(从iOS设备)上载到用户的twitter帐户。推文文本上传到推文中,但图像从未出现。响应状态代码为200 显然,基本功能正常,否则tweet将根本不会显示在用户流中,我已经检查了图像是否找到,以及(NSData)变量是否包含预期的字节数 查看下面的代码-正如您从注释代码中看到的,我尝试了各种不同的修改,但没有成功。。。感谢您的任何想法/帮助 - (void) postToTwitter:(NSString *)postMessage withIm
- (void) postToTwitter:(NSString *)postMessage withImageNamed:(NSString *)imageName {
// Get Twitter account
ACAccountStore *smAccount = [[ACAccountStore alloc] init];
ACAccountType *smType = [smAccount accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSDictionary *postOptions = nil;
// Attempt to post the postMessage
[smAccount requestAccessToAccountsWithType:smType options:postOptions completion:^(BOOL accessGranted, NSError *error) {
// Access code block
if (accessGranted) {
NSLog(@"Access permitted");
NSArray *smAccountList = [smAccount accountsWithAccountType:smType];
if ([smAccountList count] > 0) {
// Get the first Twitter account
ACAccount *postingAccount = [smAccountList lastObject];
// Create the post request
//SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:postMessage forKey:@"status"]];
//SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://upload.twitter.com/1.1/media/upload.json"] parameters:nil];
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:nil];
// Add image to post
// TODO - Check which to use >
UIImage *postImage = [UIImage imageNamed:imageName];
//UIImage *postImage = [UIImage imageWithContentsOfFile:imageName];
//NSData *imageData = UIImageJPEGRepresentation (postImage, 0.5f); // Set compression
NSData *imageData = UIImagePNGRepresentation(postImage);
//[postRequest addMultipartData:imageData withName:@"media" type:@"image/png" filename:@"image.png"];
[postRequest addMultipartData:imageData withName:@"media[]" type:@"multipart/form-data" filename:@"image.png"];
// Add text
[postRequest addMultipartData:[postMessage dataUsingEncoding:NSUTF8StringEncoding] withName:@"status" type:@"multipart/form-data" filename:nil];
// Execute the post
[postRequest setAccount:postingAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// Post code block
if (error) {
NSLog(@"Post failed - %@", error);
if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)])
[_delegate didNotPost:self];
} else {
NSLog(@"Post complete");
if (_delegate && [_delegate respondsToSelector:@selector(didPost:)])
[_delegate didPost:self];
}
}];
}
} else {
NSLog(@"Access denied - %@", error);
if (_delegate && [_delegate respondsToSelector:@selector(didNotPost:)])
[_delegate didNotPost:self];
}
} ];
}
以下是回应:
(lldb) po urlResponse
<NSHTTPURLResponse: 0x7fb9bf80> { URL: https://api.twitter.com/1.1/statuses/update.json } { status code: 200, headers {
"Cache-Control" = "no-cache, no-store, must-revalidate, pre-check=0, post-check=0";
"Content-Disposition" = "attachment; filename=json.json";
"Content-Encoding" = gzip;
"Content-Length" = 751;
"Content-Type" = "application/json;charset=utf-8";
Date = "Thu, 02 Apr 2015 17:14:20 GMT";
Expires = "Tue, 31 Mar 1981 05:00:00 GMT";
"Last-Modified" = "Thu, 02 Apr 2015 17:14:20 GMT";
Pragma = "no-cache";
Server = "tsa_b";
"Set-Cookie" = "lang=en";
Status = "200 OK";
"Strict-Transport-Security" = "max-age=631138519";
"x-access-level" = "read-write";
"x-connection-hash" = 6f61f3c96ba990931cca3fa950a42b38;
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-response-time" = 51;
"x-transaction" = b2c77803db7e996d;
"x-tsa-request-body-time" = 426;
"x-twitter-response-tags" = BouncerCompliant;
"x-xss-protection" = "1; mode=block";
} }
(lldb)po URL响应
{URL:https://api.twitter.com/1.1/statuses/update.json }{状态代码:200,标题{
“缓存控制”=“无缓存,无存储,必须重新验证,预检查=0,后检查=0”;
“内容处置”=“附件;文件名=json.json”;
“内容编码”=gzip;
“内容长度”=751;
“内容类型”=“应用程序/json;字符集=utf-8”;
日期=“2015年4月2日星期四17:14:20 GMT”;
Expires=“1981年3月31日星期二05:00:00 GMT”;
“上次修改”=“2015年4月2日星期四17:14:20 GMT”;
Pragma=“无缓存”;
Server=“tsa_b”;
“设置Cookie”=“lang=en”;
Status=“200正常”;
“严格的交通安全”=“最大年龄=631138519”;
“x访问级别”=“读写”;
“x-connection-hash”=6f61f3c96ba990931cca3fa950a42b38;
“x-content-type-options”=nosniff;
“x-frame-options”=SAMEORIGIN;
“x响应时间”=51;
“x交易”=B2C77803B7E996D;
“x-tsa-request-body-time”=426;
“x-twitter-response-tags”=BouncerCompliant;
“x-xss-protection”=“1;模式=块”;
} }
所以在经历了很多痛苦之后,我意识到我发布到了错误的twitter API。下面的修改完成了这项工作
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"] parameters:nil];
在我发布这篇文章时,不推荐使用\u媒体更新\u。虽然它现在可以工作,但将来可能会突然停止-这可能会使你的应用程序在将来无法工作 在阅读了很多Twitter的API文档之后,我成功地使它工作了,而没有使用“用媒体更新” 上载图像(或视频)所涉及的过程是:
有关详细代码,请参见我的教程博客:但不推荐使用\u媒体更新\u。我想它现在可以工作了,但将来会停止。仅供参考,我也面临着与您相同的问题-使用/statuses/update.json只发布文本,而不是多部分图像。请注意,媒体上载路径为:
https://upload.twitter.com/1.1/media/upload.json
它的子域与状态路径不同
NSDictionary *parameters = @{@"status": @"See my image:",
@"media_ids": mediaID };