Loading an Image with AFNetworking 2.0

风格不统一 提交于 2019-12-30 06:32:06

问题


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

}

Thanks a bunch


回答1:


I use the AFNetworking in this way :

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;

My client is created earlier but it's created like that.

I hope that will help.




回答2:


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

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 (:



来源:https://stackoverflow.com/questions/21789169/loading-an-image-with-afnetworking-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!