前几天用AF作网络请求,在上传图片的时候遇到了一些问题。。
- (void)postRequsetUploadImage:(NSString *)url Dic:(NSDictionary *)dic image:(UIImage *)image
{
NSString * strUrl = [HostAddress stringByAppendingString:url];
appDelegate = [UIApplication sharedApplication].delegate;
// 设置请求格式
[appDelegate.manager POST:strUrl parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *fileName = [NSString stringWithFormat:@"%@.png",[dic objectForKey:@"Realname"]];
[formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"Data" fileName:fileName mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
if (_dataBlock) {
_dataBlock(result,nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertView * ar = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络错误,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[window addSubview:ar];
[ar show];
hub =(MBProgressHUD *)[window viewWithTag:5000];
if (hub) {
[hub removeFromSuperview];
}
NSLog(@"出现错误: %@", error);
}];
}
代码没有问题 ,但是在上传的时候,一直是未响应,网上查了很多,有很多人说是af2.0上传的类有点问题,导致不能上传。然后下载了很多,终于找到了一个。。
下载链接: http://download.csdn.net/detail/think_ma/8210275
原文:http://blog.csdn.net/think_ma/article/details/41623965