使用 AFNetworking 2.0 加载图像
Posted
技术标签:
【中文标题】使用 AFNetworking 2.0 加载图像【英文标题】:Loading an Image with AFNetworking 2.0 【发布时间】:2014-02-14 20:43:08 【问题描述】:我正在尝试使用 AFNetworking 2.0 将照片添加到 POST。 这个 ios 应用程序将帖子和照片发送到博客。 我不知道为什么图片无法加载。
这是我目前得到的:
// publish text and image
-(void)publishTextAndImage:(NSString*)resultDisplay and:(NSString*)subject with:(NSString*)nonce
imageData = UIImageJPEGRepresentation(selectedImage, 0.7); // create a data object from selected image
NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID
NSString *formatString = [NSString stringWithFormat:@"<img src=\"/wp-content/uploads/%@\"/>",myUUID];
NSString *contentString = [formatString stringByAppendingString:resultDisplay];
NSString *moodString = [NSString stringWithFormat:@"%d",self.moodNumber];
NSDictionary *parameters = @@"title":subject,
@"content":contentString,
@"status":@"publish",
@"author":@"wordpress",
@"user_password":@"xrayyankee",
@"nonce":nonce,
@"categories":moodString,
@"attachment":@"image/jpeg";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://thrills.it/?json=posts/create_post"
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
if (selectedImage)
[formData appendPartWithFileData:imageData name:@"photo" fileName:myUUID mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"JSON: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
非常感谢
【问题讨论】:
您收到错误了吗? 我的项目也有同样的问题..您不需要更改发布图像的简单代码..但您需要更改 web 服务的代码以将图像发布到特定的 wordpress 帖子.. 【参考方案1】:我以这种方式使用 AFNetworking:
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://thrills.it/?json=posts"]];
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"create_post" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
if (selectedImage)
[formData appendPartWithFileData:imageData name:@"photo" fileName:myUUID mimeType:@"image/jpeg"];
];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
NSLog(@"JSON: %@", responseObject);
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
NSLog(@"Error: %@", error);
];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
float progressValue = (float)((double)totalBytesWritten/(double)totalBytesExpectedToWrite);
NSLog(@"%f", progressValue);
];
[self.queue addOperation:operation];
.
@property (nonatomic, strong) NSOperationQueue *queue;
我的客户端是之前创建的,但它是这样创建的。
我希望这会有所帮助。
【讨论】:
谢谢,尼古拉斯。 AFNetworking 2.0 中不再存在 AFHTTPClient。我会降级到 1.0 并试一试,但我们只是设法解决了这个问题。以不同的方式命名参数是一个愚蠢的问题。无论如何,谢谢并为你投票。【参考方案2】:我知道了,代码很好,是参数命名有问题,这里是:
// publish text and image
-(void)publishTextAndImage:(NSString*)resultDisplay and:(NSString*)subject with: (NSString*)nonce
imageData = UIImageJPEGRepresentation(selectedImage, 0.7); // create a data object from selected image
NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID
NSString *formatString = [NSString stringWithFormat:@"<img src=\"/wp- content/uploads/%@\"/>",myUUID];
NSString *contentString = [formatString stringByAppendingString:resultDisplay];
NSString *moodString = [NSString stringWithFormat:@"%d",self.moodNumber];
NSDictionary *parameters = @@"title":subject,
@"content":contentString,
@"status":@"publish",
@"author":@"wordpress",
@"user_password":@"xrayyankee",
@"nonce":nonce,
@"categories":moodString;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://thrills.it/?json=posts/create_post"
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
if (selectedImage)
[formData appendPartWithFileData:imageData name:@"attachment" fileName:myUUID mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"JSON: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
所以基本上我从参数字典中删除了“附件”参数,并将附加的 imageData 的名称更改为@“附件”。这是 wordpress json api 非常挑剔的问题(:
【讨论】:
以上是关于使用 AFNetworking 2.0 加载图像的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 2.0 - 在监控进度的同时批量下载图像
在单个 PHP 中使用 AFNetworking 2.0 上传图像和信息(两者)
如何在上传带参数的图像时使用 AFNetworking 2.0 设置 cookie?