简体   繁体   中英

UIImage from URL take too much time to appear

I'm trying to loading an image from an URL. The loading works fine but the image take too much time to appear. Sometimes, I need to switch between tab (with my tab bar controller) to see the news images. So, I'm looking for a way to speed up its loading to imageview.

        let discoverTask = URLSession.shared.dataTask(with: discoverUrl!) { (data, response, error) in
        if error != nil {
            print(error as Any)
        } else {

            let jsonString: String = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)! as String

            if let data = jsonString.data(using: .utf8) {

                if let json = try? JSON(data: data) {

                    for item in json["results"].arrayValue {
                        posterURLString = posterBaseURL + item["poster_path"].stringValue
                        discoverPosters.append(posterURLString)
                    }

                    var i = 0
                    while(i < discoverPosters.count)
                    {
                        let posterURL = URL(string: discoverPosters[i])!
                        discoverPostersData.append(try! Data(contentsOf: posterURL))
                        i = i+1
                    }

//I'm setting the new picture from the URL

                    let toDiscoverPoster1 = UIImage(data: discoverPostersData[0])
                    UIView.transition(with: self.discoverPoster1,
                                      duration:1,
                                      options: .transitionCrossDissolve,
                                      animations: { self.discoverPoster1.image = toDiscoverPoster1 },
                                      completion: nil)

                    let toDiscoverPoster2 = UIImage(data: discoverPostersData[1])
                    UIView.transition(with: self.discoverPoster2,
                                      duration:1,
                                      options: .transitionCrossDissolve,
                                      animations: { self.discoverPoster2.image = toDiscoverPoster2 },
                                      completion: nil)

                    let toDiscoverPoster3 = UIImage(data: discoverPostersData[2])
                    UIView.transition(with: self.discoverPoster3,
                                      duration:1,
                                      options: .transitionCrossDissolve,
                                      animations: { self.discoverPoster3.image = toDiscoverPoster3 },
                                      completion: nil)

                }
            }
        }
    }

PS : Sorry for my bad English...

First you have 2 nested requests

Second you load the images like this in a while loop

discoverPostersData.append(try! Data(contentsOf: posterURL))

which is not a good way as it blocks the main thread , you have to load them asynchronously in a back ground queue

Third the completion of URLSession.shared.dataTask runs in background queue , so you need to use when setting the images to the UIImageViews

DispatchQueue.main.async {

  // to set the images 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM