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?
Asked
Active
Viewed 4,917 times
3 Answers
7
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
-
That code only allows me to paste the image into UIPasteboard, what I need is to paste from UIPasteboard. I don't want to ask the user to paste it manually. – Carlos Mar 16 '16 at 20:41
-
Casillas, can you tell me if this line is the right translation to swift? var pasteData: NSData = UIPasteboard.generalPasteboard().dataForPasteboardType(String(kUTTypeJPEG)) – Carlos Mar 17 '16 at 01:18
2
Swift 3
If an imaged has been copied from another application (e.g. Safari) this is how I add it into my app. I trigger this from a UIAlertController as a UIAlertAction.
let pasteboard = UIPasteboard.general
if pasteboard.hasImages {
myImage.image = pasteboard.image
}

Steve Thoonen
- 126
- 3
0
Alternatively, you may use NSData. It works on Simulator too.
// Copy
let image = UIImage(named: "sample")
let imageData = UIImageJPEGRepresentation(image!, 1)
UIPasteboard.general.setData(imageData!, forPasteboardType: "image")
// Paste
let imageData = UIPasteboard.general.data(forPasteboardType: "image")
let image = UIImage(data:imageData!)
copiedImage.image = image

Stanley Yong
- 1,844
- 1
- 12
- 7