16

I tried to use the following code to copy the Image data to UIPasteboard on click of the Copy item in the menu.

UIPasteboard *gpBoard = [UIPasteboard generalPasteboard];
[[gpBoard setData:UIImageJPEGRepresentation(imgView.image, 1.0) forPasteboardType:UIPasteboardTypeListImage];

Which parameter i have to send for forPasteboardType:, and how to test after copying the data?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
pradeepa
  • 4,104
  • 5
  • 31
  • 41

3 Answers3

31

It's a lot easier:

[UIPasteboard generalPasteboard].image = imgView.image;

To get the image out again:

UIImage *image = [UIPasteboard generalPasteboard].image;
omz
  • 53,243
  • 5
  • 129
  • 141
  • 3
    Thanks alot, how do i test? should i get the image out and set it to some ImageView? – pradeepa Jul 11 '11 at 07:10
  • @pradeepa You can open iMessage or whatsapp or facebook Messenger and paste the image. If you see your copied image then its done ^_^ – Pradeep Mittal Feb 03 '16 at 10:24
  • I'm not seeing it get pasted, I'm trying to paste it to a UITextView, anything I gotta set for it? – C0D3 Jun 14 '16 at 18:25
3

I tried the above and for some reason it's a hit and miss on iOS 7,8. Sometimes it works and sometimes it doesn't i.e. system doesn't show "Paste" option.

This worked for me consistenly on all devices and all iOS

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:@"test.png"]);
    [pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
Sam B
  • 27,273
  • 15
  • 84
  • 121
3

Swift:

UIPasteboard.generalPasteboard().image = imageToCopy
DanielZanchi
  • 2,708
  • 1
  • 25
  • 37