2017-03-01 5 views
0

私のアプリにページビューコントローラーがあり、このページビューコントローラーには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() 
} 
} 

ここではそれらの1を書き、私のビューコントローラクラスのすべてにこのコードをコピーしてここにあります私の物語の絵ボード

as you see in the picture I have Page VC2 that is page view controller and four view controllers that connected to the page VC2

答えて

0

複数のVCを所有しているという事実は、画像をダウンロードする作業とほとんど関係ありません。

ただし、アブストラクションの欠如のために、適切なトラックにあります。URLSessionURLSessionTaskを使用することは、Apple APIを使用して行う正しい方法です。

あなたは以下に興味があるかもしれません。抽象的なものに多くの開発者がAlamofireという名前のこのサードパーティのライブラリを使用し、具体的にはケースAlamofireImageを使用します。

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) 
+0

を行う必要があるだろう、それは辞書を探していますか?か否か? –

関連する問題