iOS 下载图片 -当后台返回的数据是文件流而不是图片url
2016-04-08 13:49
651 查看
NSString *urlStr = [NSString stringWithFormat:@"%@uploadFileRest/downFile",[AFAppDotNetAPIClient getUrl]];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *post = [NSString stringWithFormat:@"contentType=%@&fileName=%@&filePath=%@",model.CONTENT_TYPE,model.FILE_NAME,model.FILE_PATH];
request.HTTPBody = [post dataUsingEncoding:NSUTF8StringEncoding];
NSURLSession *session = [NSURLSession sharedSession];
// 可以不必创建请求直接用url进行获取,但是只能应用于get请求
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:error.localizedDescription preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
}];
[alert addAction:okAction];
//弹出提示框;
[self presentViewController:alert animated:true completion:nil];
}else
{
image1 = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^
{
// 更UI
UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
// imgView.backgroundColor = [UIColor yellowColor];
imgView1.image = image1;
[self.view addSubview:imgView1];
});
}
}];
[task resume];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *post = [NSString stringWithFormat:@"contentType=%@&fileName=%@&filePath=%@",model.CONTENT_TYPE,model.FILE_NAME,model.FILE_PATH];
request.HTTPBody = [post dataUsingEncoding:NSUTF8StringEncoding];
NSURLSession *session = [NSURLSession sharedSession];
// 可以不必创建请求直接用url进行获取,但是只能应用于get请求
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:error.localizedDescription preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
}];
[alert addAction:okAction];
//弹出提示框;
[self presentViewController:alert animated:true completion:nil];
}else
{
image1 = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^
{
// 更UI
UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
// imgView.backgroundColor = [UIColor yellowColor];
imgView1.image = image1;
[self.view addSubview:imgView1];
});
}
}];
[task resume];
相关文章推荐
- iOS开发:创建真机调试证书
- iOS svn提交静态库.a文件
- runLoop 添加timer的正确方式 (ios oc)
- ios多工程依赖
- iOS开发- NSNumber处理
- iOS 判断只有数字、小数点和减号
- ios 怎么添加pch预编译文件
- iOS 使用GitHub托管代码(github desktop使用)
- 苹果开发者证与真机调试
- ios之单例模式
- iOS实现自定义的弹出视图(popView)
- iOS知识小集 第四期
- iOS知识小集 第三期
- iOS知识小集 第二期
- iOS 获取设备或系统信息
- iOS中设置导航栏标题的字体颜色和大小
- iOS知识小集 第一期
- iOS中定时任务的三种方法
- iOS 开发学习资料整理
- NSInvocation