AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *imagePostUrl = [NSString stringWithFormat:@"www.abc.com?"];
NSDictionary *parameters = @{@"device_id":string,@"device_phone":[[NSUserDefaults standardUserDefaults] objectForKey:@"SerialNumber"]};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:imagePostUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"uploaded_file" fileName:ext mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
op.responseSerializer = [AFHTTPResponseSerializer serializer];
[[NSOperationQueue mainQueue] addOperation:op];
我从手机中的图像路径中的图像获取图像,我将所有带参数的图像发送到服务器。这里发送图像是一个接一个的参数到服务器锄头我可以发送这个。 我怎样才能做到这一点?请帮我。提前谢谢。
答案 0 :(得分:0)
上传图片的常用方法是使用多部分表单编码。请阅读answer关于使用multi-part form data
上传图片的信息。如果您使用AFNetworking,则可以使用Multi-Part Request完成。
使用JSON,您可以使用简单的base64编码字符串:
UIImage *image = [[UIImage alloc] init]; //your image here
NSData *data = UIImagePNGRepresentation(image);
NSString *base64string = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
为每个图像执行此操作,并以JSON格式发送字符串数组。在服务器端,您需要将此字符串解码为图像。但 是一个很好的解决方案。
答案 1 :(得分:0)
是的,
我会建议同样的。
AFNetworking
会像你这样为你做这件事:
-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" 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];
}