Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show a GIF image using NSImageView

Tags:

swift

cocoa

I want to show an animated gif image in my cocoa application.

I dragged the gif into Assets.xcassets in XCode. I was hoping that NSImageView can show a gif out of the box, so I tried the following code.

let imageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 512, height: 512))
imageView.canDrawSubviewsIntoLayer = true
imageView.imageScaling = .ScaleNone
imageView.animates = true
imageView.image = NSImage(named: "loading-animation")

window.contentView?.addSubview(imageView)

The image does not show up. The above code works with a png image. How do I get this to work?

like image 506
RookiePro Avatar asked Dec 14 '22 06:12

RookiePro


1 Answers

For me it works only if there is nothing set in the Attribute Inspector and with the code like that (hope, it will be useful for someone):

class FirstViewController: NSViewController {  

  @IBOutlet weak var imgImage1: NSImageView!

 override func viewDidLoad() {
        super.viewDidLoad()

let imgImage1 = NSImageView(frame: NSRect(x: 407, y: 474, width: 92, height: 74))
        imgImage1.canDrawSubviewsIntoLayer = true
        imgImage1.imageScaling = .scaleProportionallyDown
        imgImage1.animates = true
        imgImage1.image = NSImage(named: "mygif")
        self.view.addSubview(imgImage1)
}

Enjoy :)

like image 174
J.A. Avatar answered Jan 20 '23 18:01

J.A.