AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
//设置返回的数据解析格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//以下为上传的一些参数,根据服务器网址要求,可以有可以没有
NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"myUid"];
NSString *valueWWW= [NSString stringWithFormat:@"{"uid":"%@"}",uid];
NSMutableDictionary *ssdict = [[NSMutableDictionary alloc] init];
[ssdict setValue:valueWWW forKey:@"home"];
//post方法进行图片上传
AFHTTPRequestOperation *operation = [manager POST:@"http://public.aiweiker.com/index.php?g=App&m=HomePage&a=updateBg" parameters:ssdict constructingBodyWithBlock:^(id<</span>AFMultipartFormData> formData) {
//将图片装换为二进制格式--UIImageJPEGRepresentation第一个参数为要上传的图片,第二个参数是图片压缩的倍数
//如果要上传多张图片把下面两句代码放到for循环里即可
for (int i = 0; i<</span>0; i++) {
NSData *imageData =UIImageJPEGRepresentation(image, 0.1);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"anyImage_%d",i]fileName:[NSString stringWithFormat:@"anyImage_%d.jpg",i] mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//成功的返回--进行你的操作
NSDictionary *dict = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败的返回
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}];
//获得上传进度
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"百分比:%f",totalBytesWritten*1.0/totalBytesExpectedToWrite);
}];