1

I want to load an array of images before the app show its views to user, with a custom delay for example 5 milliseconds . any help ??

I've used UIImageView in UIViewController and this is my code:

import UIKit

class CustomViewController: UIViewController {


@IBOutlet weak var imageView: UIImageView!

var logoImage: [UIImage] = [
    UIImage(named: "splash_logo_1.png")!,
    UIImage(named: "splash_logo_2.png")!,
    UIImage(named: "splash_logo_3.png")!,
    UIImage(named: "splash_logo_4.png")!,
    UIImage(named: "splash_logo_5.png")!,
    UIImage(named: "splash_logo_6.png")!,
    UIImage(named: "splash_logo_7.png")!,
    UIImage(named: "splash_logo_8.png")!,
    UIImage(named: "splash_logo_9.png")!,

]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8 , execute: {
        // Put your code which should be executed with a delay here
        for i in 1..<self.logoImage.count
        {
            let image = self.logoImage[i]
            self.imageView.image = image
        }
    })

}

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

after building the app, it only shows the last image (plash_logo_9.png).

so where is the problem ..?

MEH
  • 1,777
  • 3
  • 15
  • 34

2 Answers2

2

//Try this,

override func viewDidLoad() {
    super.viewDidLoad()

    imageView.tag = 0
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8 , execute: {
        self.changeImage()
    })
}

func changeImage() {
    let image = self.logoImage[imageView.tag]
    self.imageView.image = image

    imageView.tag += 1
    if imageView.tag == logoImage.count {
        //stop the animation
        //Show its views to user
        return
    }

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8 , execute:
    {
        self.changeImage()
    })
}
Berlin Raj
  • 124
  • 6
1

You are starting to display the images too soon for the user. viewDidLoad only means, that your viewController has been loaded to the memory, and ready for initial setup, however, it is not visible for the user.

In order to make the presentation visible to the user, i would recommend to start displaying it in viewDidAppear, and only set the first image in viewDidLoad.

At the end, you should change the for and dispatch calls order. You need to create as many dispatch blocks, with a delay constantly increased, as many images you have.

So, remove viewDidLoad and replace it with this:

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        var delay: Double = 0 
        for i in 0..<self.logoImage.count {
            DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
                // Put your code which should be executed with a delay here
                let image = self.logoImage[i]
                self.imageView.image = image
            })
            delay += 0.8
        }
    }

Furthermore, i would recommend to read more about the viewControllers lifecycle.

Community
  • 1
  • 1
dirtydanee
  • 6,081
  • 2
  • 27
  • 43
  • I put my code in viewDidAppear and the first image in viewDidLoad but still the same problem. – MEH Jan 06 '17 at 10:04
  • thanks @dirtydanee but for what did you do (delay += 0.8) when I made it (delay =0.8) the show gone crazy fast ? – MEH Jan 06 '17 at 11:51
  • well, if you do not increase the delay, than each dispatch queue will be fired at the same time. exactly 0.8 s from `now()`. But, if you keep increasing the delay between the queues, than the first queue will be fired with 0.8 delay, second with 0.8 + 0.8, third 1.6+0.8, etc... So this way, you will see the images are changing with a certain delay. To speed up decrease, to slow down the increase 0.8, but always keep adding to each other. – dirtydanee Jan 06 '17 at 13:38
  • I made it 0.01 it's fast but I don't know there's something chopping the frames, any suggestions ? – MEH Jan 06 '17 at 13:59
  • what do you mean by chopping the frames? – dirtydanee Jan 06 '17 at 14:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132502/discussion-between-mohammad-eliass-alhusain-and-dirtydanee). – MEH Jan 06 '17 at 14:07