AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript", nil];
//可以多张图片也可以是单张,如果是多张而且后台开了异步上传的话在这儿直接做for循环要保证上传的时候fileName不同。
NSString *urlString = [NSString stringWithFormat:@"%@%@",DEBUG_URL,@"mobile/explorer"];
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
//上面的这三句就是获取当前时间的,目的就是让上传的名字不同。
// for (int i =1; i<=8; i++) {这里注释掉的就是如果你要多张上传的时候用的
NSData *data = UIImageJPEGRepresentation(你的图片UIimage,0.7);
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:data name:@"mf" fileName:fileName mimeType:@"image/jpg"];
//}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//这里就是处理上传成功后的返回等操作,大家可以不用看我的。我在这儿是把成功后,后台返回给我的图片id返回给我的主控制器
// CGLog(@"Success: %@", responseObject);
if ([responseObject[@"status"] integerValue]==0) {
[SVProgressHUD showSuccessWithStatus:@"上传成功。"];
//店铺图片多张
if (isMore==YES) {
[imageArr addObject:[NSString stringWithFormat:@"%@",responseObject[@"data"][@"s_url"]]];
if (imageArr.count == arr.count) {
NSString *imagesId = [imageArr componentsJoinedByString:@","];
// CGLog(@"%@",imagesId);
if (self.delegate && [self.delegate respondsToSelector:@selector(postMoreImageId:)]) {
[self.delegate postMoreImageId:imagesId];
}
}
//门头照
}else{
if (self.delegate && [self.delegate respondsToSelector:@selector(postImageId:)]) {
[self.delegate postImageId:[NSString stringWithFormat:@"[%@]",responseObject[@"data"][@"id"]]];
}
}
}else{
[SVProgressHUD showErrorWithStatus:@"上传失败。"];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//上传失败在这儿处理
// CGLog(@"Error: %@", error);
[SVProgressHUD showErrorWithStatus:@"上传失败,请确认网络是否连接。"];
}];
另外有时候上传服务器后,照片要在PC上展示,会出现照片在PC上显示是旋转了90度的,那C哥(我的自称)就搜了一个现成的方法可供大家参考,已经试过了管用:http://www.cnblogs.com/caodedi-88/p/6256347.html