iOS 下載圖片 -當后台返回的數據是文件流而不是圖片url


 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];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM