Run a background thread on Swift 3

33,472

Solution 1

It's OK to load image on the background, but it's not OK to perform UI updates on background thread. That's why the function must contain two threads.

func setupImageViewWithURL(url: URL) {
    var image: UIImage? = nil

    DispatchQueue.global().async { 
        do {
            try image = UIImage(data: Data(contentsOf: url))!
        } catch {
            print("Failed")
        }
        DispatchQueue.main.async(execute: {
            if image != nil {
                image = self.imageWithImage(sourceImage: image!, scaledToWidth: UIScreen.main.bounds.size.width)
                self.imageImageView.image = image
                self.imageImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (image?.size.height)!)
            }
        })
    }
}

Solution 2

Swift 4.0

func setupImageViewWithURL(url: URL) {

    var image: UIImage? = nil
    DispatchQueue.global(qos: .background).async {
        do {
            try image = UIImage(data: Data(contentsOf: url))!
        } catch {
            print("Failed")
        }
        DispatchQueue.main.async {
            if image != nil {
                image = self.imageWithImage(sourceImage: image!, scaledToWidth: UIScreen.main.bounds.size.width)
                self.imageImageView.image = image
                self.imageImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (image?.size.height)!)
            }
        }
    }
}
Share:
33,472

Related videos on Youtube

FS.O6
Author by

FS.O6

Updated on September 29, 2020

Comments

  • FS.O6
    FS.O6 over 3 years

    I have a function that goes like this:

    fileprivate func setupImageViewWithURL(url: URL) {
        var image: UIImage? = nil
        do {
            try image = UIImage(data: Data(contentsOf: url))!
        } catch is NSError {
            print("Failed")
        }
    
        image = self.imageWithImage(sourceImage: image!, scaledToWidth: UIScreen.main.bounds.size.width)
        self.imageImageView.image = image
        self.imageImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: (image?.size.height)!)
    }
    

    I want to run it on a Background thread.

    I've tried the GDC methods of Swift2, but it didn't work.

    Did anything change in the thread topic in Swift3?

    Thank you!

    • Sachin Vas
      Sachin Vas over 7 years
    • FS.O6
      FS.O6 over 7 years
      @New16 I've already tried it, it doesn't even complies
    • Martin R
      Martin R over 7 years
    • Sachin Vas
      Sachin Vas over 7 years
      @FS.O6 I am using it in my code. I don't know the way you are implementing it. Show what error you are getting.
    • Max Pevsner
      Max Pevsner over 7 years
      You don't want to run it on background thread. UI updates should be never performed on background threads.
    • FS.O6
      FS.O6 over 7 years
      @MaxPevsner I want to load the image on the background because it's very slow now
    • Max Pevsner
      Max Pevsner over 7 years
      @FS.O6, see my answer below.
  • FS.O6
    FS.O6 over 7 years
    ImageWithImage is a method that crops the image, can it be done on a background thread?
  • Max Pevsner
    Max Pevsner over 7 years
    @FS.O6, I don't think it should be problem. You can test it.
  • FS.O6
    FS.O6 over 7 years
    Ok, great. Thanks!
  • HMD
    HMD about 6 years
    What is this answer add to the already accepted answer? To me it's just a copy-paste with minimal change.