2017-05-25 10 views
1

私はSwift開発には新しく、今は完全に失われています。私は簡単なラベルを作ってストーリーボードに置いた。私のコードはそれとは全く交わりませんが、デフォルトでそこに配置された他のUILabelsとは異なり、ロードに8〜30秒かかります。どうしてこれなの?Swift:UILabelとUIImageViewはロードに8-30秒かかる

URLから引き出された画像で同じ問題が発生します。直ちにイメージをダウンロードしてUIImageViewに割り当てますが、実際に更新するには8〜30秒かかりますが、理由はわかりません。ここで

は私のコントローラのコード がFirstViewController.swift

ある
import UIKit 

class FirstViewController: UIViewController { 
    @IBOutlet var profilePicture: UIImageView! 
    var currentUser: User! 
    @IBOutlet var profileText: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Declare currentUser variable 
     currentUser = User(email: "", first_name: "", last_name: "", department: "", admin: false, superadmin: false, thumb_url: "") 

     // Load timesheet structs 
     getTimesheetPages() 

     // Load user struct and wait for it to finish 
     getUserDetails() { 
      self.downloadProfilePic() 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func downloadProfilePic() { 
     let regURL = "http://localhost:3000" 
     let profilePicURL = URL(string: regURL + currentUser.thumb_url)! 

     let session = URLSession(configuration: .default) 

     // Define a download task 
     let downloadPicTask = session.dataTask(with: profilePicURL) { (data, response, error) in 
      // The download has finished. 
      if let e = error { 
       print("Error downloading profile picture: \(e)") 
      } else { 
       // No errors found. 
       if let res = response as? HTTPURLResponse { 
        print("Downloaded profile picture with response code \(res.statusCode)") 
        if let imageData = data { 
         print("Enters image load") 

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

         // Do something with your image. 
         self.profilePicture.image = image 

         // Initialize profile picture properties 
         self.profilePicture.layer.borderWidth = 1.0 
         self.profilePicture.layer.masksToBounds = false 
         self.profilePicture.layer.borderColor = UIColor.white.cgColor 
         self.profilePicture.layer.cornerRadius = self.profilePicture.frame.size.width/2 
         self.profilePicture.clipsToBounds = true 

        } else { 
         print("Couldn't get image: Image is nil") 
        } 
       } else { 
        print("Couldn't get response code for some reason") 
       } 
      } 
     } 

     downloadPicTask.resume() 
    } 

// more functions 

ここで私はここで Image of Storyboard

で働いているもののイメージがある最初〜10秒間何私のエミュレータが表示され Image of Emulator

+4

あなたのUIの更新がメインスレッド – dan

+0

どのようにこれを行うに派遣する必要が完璧に働いていましたか?私はそれをDispatchQueue.main.async {}でラップし、うまくいきませんでした。 main.syncは動作しませんでした。 – Holabola

+2

'DispatchQueue.main.async { self.profilePicture.image = image; //残りのUIアップデートコード } ' –

答えて

2

Leoが言ったように、私は関数呼び出しをすべてUIの代わりにDispatchQueue.main.async { }にラッピングしていましたpdateコール。

DispatchQueue.main.async { 
    self.profilePicture.image = image 
    // rest of UI update code 
} 

self.profilePicture.image = imageを変更することにより

はそれが

関連する問題