1

I am a beginner to Swift, so I would probably need some explaining. Please bear with me. I am creating an app that needs to load a .gif file, but when I run it on the simulator it loads for one second (or even less) and then becomes blank, but the screen itself stays on the page that had the .gif file on it. I'm not sure why this happens. Can someone explain? Thank you!

Here is the code:

import UIKit

class ViewController: UIViewController {


    @IBAction func Burning(sender: AnyObject) {

        let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("fire", withExtension: "gif")!)
        let imageGif = UIImage.gifWithData(imageData!)
        let imageView = UIImageView(image: imageGif)
        imageView.frame = CGRect(x: 0.0, y:0.0, width:414.0, height: 685.0)
        view.addSubview(imageView)
    }

    @IBAction func Smashing(sender: AnyObject) {


        let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("water", withExtension: "gif")!)
        let imageGif = UIImage.gifWithData(imageData!)
        let imageView = UIImageView(image: imageGif)
        imageView.frame = CGRect(x: 0.0, y:0.0, width:414.0, height: 685.0)
        view.addSubview(imageView)
    }

    @IBAction func Shredding(sender: AnyObject) {


        let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("shredder", withExtension: "gif")!)
        let imageGif = UIImage.gifWithData(imageData!)
        let imageView = UIImageView(image: imageGif)
        imageView.frame = CGRect(x: 0.0, y:0.0, width:414.0, height: 685.0)
        view.addSubview(imageView)
    }

     override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.
    }

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


}
holex
  • 23,961
  • 7
  • 62
  • 76
Cindy Kim
  • 31
  • 1
  • 4
  • There is library called AsyncImageView. it takes a gif and show it to u, I recommend you to use it! – Mohamad Bachir Sidani Aug 02 '16 at 15:47
  • I'd recommend this: https://github.com/kirualex/SwiftyGif – Konsy Aug 02 '16 at 16:00
  • Possible duplicate of [Add animated Gif image in Iphone UIImageView](http://stackoverflow.com/questions/4386675/add-animated-gif-image-in-iphone-uiimageview) – SHN Aug 02 '16 at 16:06
  • @SHN I've tried all of those but they didn't work... – Cindy Kim Aug 03 '16 at 08:27
  • @CindyKim What is gifWithData method? Are you using SwiftGIf UIImage category? If yes then probably you need to share more info/code as SwiftGIF works fine, there should be some other problem with your code. – SHN Aug 03 '16 at 09:02
  • @CindyKim How did you fix this? I am running into similar issue – mshrestha Sep 14 '21 at 16:26

0 Answers0