使用AFNetworking 2.0加载图像 [英] Loading an Image with AFNetworking 2.0

查看:84
本文介绍了使用AFNetworking 2.0加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AFNetworking 2.0将照片添加到POST。
这个ios App将一张照片发布到博客上。
我无法弄清楚图片无法加载的原因。

I'm trying to add a photo to a POST using the AFNetworking 2.0. This ios App sends a post an a photo to a blog. I can'f figure out why the images don't load.

这是我到目前为止所获得的:

Here is what I got so far:

// 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);
 }];

}

非常感谢

推荐答案

我知道了,代码很好,这是参数命名的问题,这里是:

I got it, the code was fine it was an issue with the naming of the parameters, here it is:

// 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);
}];

所以基本上我从参数字典中删除了attachment参数并更改了附加的imageData的名称到@附件。这是一个wordpress json api非常挑剔的问题(:

so basically I removed the "attachment" parameter from the parameters dictionary and changed the name of the appended imageData to @"attachment". it was an issue of wordpress json api being very picky (:

这篇关于使用AFNetworking 2.0加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆