標籤:af
前幾天用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
AF上傳圖片問題