Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste gif image into NSPasteboard

Tags:

macos

swift

I am developing osx application. I want to paste an Gif image into pasteboard. How can I do that?

What I have is

  • NSImage
  • NSPasteboard

What I want to do is to paste that image into pasteboard. I am able to paste PNG image but what I need is to paste GIF image.


My existing code

let imageURL = imageObject.imageURL!
let fileName = imageURL.lastPathComponent
var saveURL = NSURL(string: "file://" + NSTemporaryDirectory())
saveURL = saveURL?.URLByAppendingPathComponent(fileName!)
// I have data now
let data = NSData(contentsOfURL: imageURL)

pasteboard.declareTypes([NSTIFFPboardType], owner: nil)
pasteboard.setData(data!, forType: "com.compuserve.gif")
like image 618
Ethan Fang Avatar asked Dec 09 '25 17:12

Ethan Fang


1 Answers

About 10 years ago same one asked How do I put a GIF onto an NSPasteboard? in an Apple discussion group and here is my answer. Although 10 years old and the NSPasteboard methods changed since that time my answer still works. I confess: my advice is a bit dirty.

like image 68
Heinrich Giesen Avatar answered Dec 12 '25 06:12

Heinrich Giesen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!