我是Swift开发的新手,现在我完全迷路了。我做了一个简单的标签,并将其放在情节提要上。我的代码完全没有与之交互,但是加载需要8至30秒,这与默认情况下放置在此处的其他UILabel不同。为什么是这样?

对于从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

这是我正在使用的图像
ios - Swift:UILabel和UIImageView加载需要8-30秒-LMLPHP

这是我的模拟器在前10秒内显示的内容
ios - Swift:UILabel和UIImageView加载需要8-30秒-LMLPHP

最佳答案

正如Leo所说,我将整个函数调用包装在DispatchQueue.main.async { }中,而不仅仅是UI更新调用。

通过将self.profilePicture.image = image更改为

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

它完美地工作

关于ios - Swift:UILabel和UIImageView加载需要8-30秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44189037/

10-13 04:22