0
我有我的應用程序頁面視圖控制器,而這個頁面視圖控制器有4個視圖控制器 - 所以我必須在所有這些頁面視圖控制器的圖像查看,我想使用JSON API下載圖像,這些網頁如何使用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()
}
}
**這將工作,但並不完全正確**
這裏是我的故事板圖片
它找字典?或不? –