iOS中的图像上传

时间:2015-07-11 07:39:15

标签: ios xcode

目前我正在ios应用程序中处理图像上传,这是我的代码

AFHTTPRequestOperationManager *man = [[AFHTTPRequestOperationManager alloc]init];
man.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

NSData *imageData = UIImagePNGRepresentation(self.imageView.image);

AFHTTPRequestOperation *op = [man POST:AddGroup parameters:@{ @"userid":@"6",  
                                                              @"name":self.txtGroupName.text
                                                              @"description":self.textViewGroupDescription.text,  
                                                              @"image":imageData }

  constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {                                        
      [formData appendPartWithFileData:imageData name:@"image" fileName:filename mimeType:@"image/png"];
   }
  success:^(AFHTTPRequestOperation *operation, id responseObject)
  {
    NSLog(@"Success: %@ ***** %@", operation.description, operation.responseString);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Imani" message:@"New Group Succesfully Created.." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
     [alertView show];                                      
  } 
  failure:^(AFHTTPRequestOperation *operation, NSError *error)
  {
     NSLog(@"Error: %@ ***** %@", operation.responseString, error);
  }];
  [op start];

现在我从json成功获得响应,但是图像数据是否正在将null传递给服务器,任何人都有想法?

谢谢。

2 个答案:

答案 0 :(得分:0)

尝试此操作以在服务器上上传图像。

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"your api link"]];
NSData *imageData = UIImageJPEGRepresentation(self.imgPost.image, 0.5);
NSDictionary *parameters = @{@"param": @"value",
                             @"param2" : @"value2"
                            };
AFHTTPRequestOperation *op = [manager POST:@"/api/store/posts/format/json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //do not put image inside parameters dictionary as I did, but append it!
    [formData appendPartWithFileData:imageData name:@"file" fileName:[NSString stringWithFormat:@"njoyful_%f.jpg",[NSDate timeIntervalSinceReferenceDate]] mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);

}];
[op start];

答案 1 :(得分:0)

我不熟悉AFHTTPRequestOperation所以我只是给你一些我用它创建json请求的东西。

- (NSURLRequest *)convertToRequest:(NSString *)stringURL withDictionary:(NSDictionary *)dictionary
{

    NSError *error = nil;

    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];

    NSURL *url = [NSURL URLWithString:stringURL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: JSONData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept-Encoding"];

    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[JSONData length]] forHTTPHeaderField:@"Content-Length"];


    return request;
}

请检查我的回答 here

这也适用于多图像上传。干杯! :)