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
Why Can't I Divide Integers in Swift
How to Hide the Navigationbar When Embedding Swiftui in Uikit
How to Change "Return" Key to "Done" on Keyboard with Swiftui
In Swift, Does Resetting the Property Inside Didset Trigger Another Didset
Swiftui Navigation on iPad - How to Show Master List
How to Get Data from Observedobject with Onreceive in Swiftui
Swift: How to Access in Appdelegate Variable from the View Controller
Sum Values of Properties Inside Array of Custom Objects Using Reduce
How to Open File Dialog with Swiftui on Platform "Uikit for MAC"
Remove Multiple Indices from Array
Firebase Says That My Rules Are Insecure, Why
How to Check Object Is Nil or Not in Swift
How to Hide the Navigation Back Button in Swiftui