Search code examples
iosswiftswift2uipasteboard

How do I paste an image programmatically with Swift?


I have a working code where I copy an image to UIPasteboard, but I cannot find a way to implement the PASTE functionality programmatically. Any idea or tip?


Solution

  • 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