#import "AFHTTPClient.h"
#import “AFHTTPRequestOperation.h"
导入 MobileCoreServices.framwork
@interface ****
{
NSOperationQueue *_queue;
}
具体实现
NSURL *url = [NSURL URLWithString:@"http://182.92.104.226:8080”];//这个地方是后台要传得服务器器地址
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation(_im, 0.5);//这个地方要问你后台支持什么格式的图片,我们后台要JPEG得
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/apitest/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {//第一个参数是POST请求,第二个是 服务器的路径 第三个是服务器需要参数的字典
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg”];//给你的图片往服务器发送并起名字
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"成功呢!json ===%@",operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];//监测系统的返回值