Swift Uipasteboard Not Copying Png

Copy Image with UIPasteBoard (Swift)

Try using this code:

let image = UIImage(named: "myimage.png")
UIPasteboard.generalPasteboard().image = image;

you can find out how this works here!

Hope this helps

Swift 5.1

UIPasteboard.general.image = image

How do I paste an image programmatically with Swift?

The following piece of code might work. Make sure you are testing on the device.

let image = UIImage(named: "person.png")
UIPasteboard.generalPasteboard().image = image;

based on the comment, you can do it as follows. I am putting here the objective-c code, I hope you could get the idea then convert it to the swift.

NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];

you may find the swift solution in the following url
Swift UIPasteboard not copying PNG

Copy GIF to UIPasteboard

For anyone who ever encounters this problem I managed to find a solution

let url: NSURL = NSBundle.mainBundle().URLForResource("\(self.imageNames[indexPath.row])", withExtension: ".gif")!
let data: NSData = NSData(contentsOfURL: url)!
UIPasteboard.generalPasteboard().setData(data, forPasteboardType: "com.compuserve.gif")

As it turns out you do need to use a URL and extract the NSData of the GIF from that URL.

Here I am getting the URL of the GIF that is in my bundle, searching for it using the name and extension of the image. I am then setting the data in the pasteboard and bingo we have an animated GIF when pasting the result from the pasteboard



Related Topics



Leave a reply



Submit