我正在尝试使用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);
}];
}
非常感谢
答案 0 :(得分:4)
我以这种方式使用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;
我的客户端是先前创建的,但它是这样创建的。
我希望这会有所帮助。
答案 1 :(得分:1)
我知道了,代码很好,这是参数命名的问题,这里是:
// 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的名称更改为@“attachment”。这是一个wordpress json api非常挑剔的问题(: