【发布时间】:2017-07-20 18:45:18
【问题描述】:
我的应用程序中有一个页面视图控制器,这个页面视图控制器有 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()
}
}
**这可行,但不完全正确**
这是我的故事板的图片
【问题讨论】:
标签: ios json api swift3 uipageviewcontroller