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?
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With