Xcode 将JPG图像上传到Twitter
我正试图上传一张jpg图片到twitter上。我可以上传@“icon.png”任意次数,但似乎无法上传jpg。我总是从twitter上收到错误403。这是我的代码:Xcode 将JPG图像上传到Twitter,xcode,image,twitter,upload,jpeg,Xcode,Image,Twitter,Upload,Jpeg,我正试图上传一张jpg图片到twitter上。我可以上传@“icon.png”任意次数,但似乎无法上传jpg。我总是从twitter上收到错误403。这是我的代码: ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:0]; UIImage *image =[UIImage imageNamed:file]; //file is the name of a jpg image. If replaced with @"icon.
ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:0];
UIImage *image =[UIImage imageNamed:file]; //file is the name of a jpg image. If replaced with @"icon.png" the upload works
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST];
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"]; //I've tried this with UIImageJPEGRepresentation(image, 80) and multipart/jpg and jpeg to no avail
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
}];
请帮忙
如果我使用下面的选项,它会工作
UIImage *image =[UIImage imageNamed:@"background.png"]; //This file is copied to the device when the app is built.
但是,如果我使用这是不起作用的,即使我已经确认这个路径和文件名没有完整地拉图像
UIImage *image =[UIImage imageNamed:[NSString stringWithFormat:@"%@/%@", dir, file]];
我做错了什么?谢谢
///解决方案
我可能是个白痴,请不要把我钉在十字架上,但这是我需要改变UIImage的地方
UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", dir, file]];
这使得PNG和JPG都可以毫无问题地上传。这是个愚蠢的错误,但至少它现在起作用了。谢谢你的回复 403错误表示错误来自Twitter;您不能从同一Twitter帐户两次共享同一内容。如果你想上传同一张图片,那么你必须先将其从Twitter帐户中删除,然后再尝试上传。否则,请上传不同的图片 检查您的
twitter帐户
,因为403
是身份验证错误。是的,我有,它设置为读写访问。该应用程序也被允许访问。正如我前面所说,我可以毫无困难地上传icon.png。当我尝试上传一个jpg(我需要的文件格式)时,它返回403。非常令人沮丧!谢谢你的及时回复!顺便说一句,我上传png的回报是200。我刚刚再次确认了这一点,但上传了另一个png。对于jpg,您是传递ImageName只是文件名,还是添加了.jpg扩展名?文件名与3521af2f5a31df18809c165096811089.jpg(实际文件名)类似。名称是实际文件数据的散列,以确保每个jpg始终是唯一的。所以要回答是的,它确实有分机。谢谢你的回复!在发布之前,请尝试验证从UIImagePNGRepresentation(image)
获取的图像不是nil
。您的jpg图像可能有问题。