Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用Api下载页面视图控制器中所有视图控制器中的图片?_Ios_Json_Api_Swift3_Uipageviewcontroller - Fatal编程技术网

Ios 如何使用Api下载页面视图控制器中所有视图控制器中的图片?

Ios 如何使用Api下载页面视图控制器中所有视图控制器中的图片?,ios,json,api,swift3,uipageviewcontroller,Ios,Json,Api,Swift3,Uipageviewcontroller,我的应用程序中有一个页面视图控制器,这个页面视图控制器有4个视图控制器,所以我在所有这些页面视图控制器中都有图像视图,我想使用Json Api下载这些页面中的图像 这是我的代码,但是我在我所有的视图控制器类中复制了这段代码,所以我只在这里写了其中一段 func refreshingphoto1() { let firstImageURL = URL(string: "http://img.autobytel.com/car-reviews/autobytel/11694-good-lo

我的应用程序中有一个页面视图控制器,这个页面视图控制器有4个视图控制器,所以我在所有这些页面视图控制器中都有图像视图,我想使用Json Api下载这些页面中的图像

这是我的代码,但是我在我所有的视图控制器类中复制了这段代码,所以我只在这里写了其中一段

func refreshingphoto1() {

    let firstImageURL = URL(string: "http://img.autobytel.com/car-reviews/autobytel/11694-good-looking-sports-cars/2016-Ford-Mustang-GT-burnout-red-tire-smoke.jpg")!

    let session = URLSession(configuration: .default)

    // Define a download task. The download task will download the contents of the URL as a Data object and then you can do what you wish with that data.


    let downloadPicTask = session.dataTask(with: firstImageURL) { (data, response, error) in
        // The download has finished.

        if error != nil {
            print("Error downloading picture")
            self.firstImageJson.image = UIImage(named: "1.jpg")
        } else {

            // No errors found.
            // It would be weird if we didn't have a response, so check for that too.
            if (response as? HTTPURLResponse) != nil {

                print("Downloaded picture with response code")

                if let imageData = data {

                    // Finally convert that Data into an image and do what you wish with it.
                    let image = UIImage(data: imageData)
                    self.firstImageJson.image = image
                    // Do something with your image.

                } else {


                    print("Couldn't get image: Image is nil")
                    self.firstImageJson.image = UIImage(named: "1.jpg")

                }
            } else {
                print("Couldn't get response code for some reason")
                self.firstImageJson.image = UIImage(named: "1.jpg")
            }
        }
    }
    downloadPicTask.resume()
}
}
**这将起作用,但并非完全正确**

这是我的故事板的图片


事实上,您有多个VC与下载图像的任务关系不大

然而,除了缺乏抽象性之外,您走的是正确的道路:使用
URLSession
URLSessionTask
是使用Apple API的正确方法

不过,您可能对以下内容感兴趣:;为了抽象一些东西,很多开发人员都会使用这个专门为您的案例命名的第三方库

例如,使用AlamofireImage,您只需执行以下操作

let imageView = UIImageView(frame: frame)
let url = URL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!

imageView.af_setImage(withURL: url, placeholderImage: placeholderImage)

它在找字典吗?还是不?