7

Update: Added bug 12408800 on Apple's site.


I am copying some one or multiple UIImage to the UIPasteboard, and it's been working like gangbusters.. until my phone upgraded to iOS 6.

  • Xcode 4.5 with iOS 5.1 - OK
  • Xcode 4.4 with iOS 6.0 - Also OK (according to this post)
  • Xcode 4.5 with iOS 6.0 - FAIL

(also tested distributing via TestFlight, for what it's worth - still fails)

Here's my code (super basic, etc):

// add image to clipboard
UIImage *image = [[UIImage imageNamed:@"testimage"];
[[UIPasteboard generalPasteboard] setPersistent:YES];
[[UIPasteboard generalPasteboard] setImage:image];

And here is what happens when I try to paste in an MMS/iMessage window (sorry for huge screenshot; retina display..):

screenshot_of_failure

..and an example of a failure on the Messages sample app in the iOS6 simulator (see the two question marks..?):

another screenshot!

Like I said, the above code has been working for ages, so I'm sure this is something new.

Any thoughts? On the linked post, the author suggests re-compiling on an old version of Xcode - but wouldn't that cause other iOS6 libraries to stop working?

toblerpwn
  • 5,415
  • 8
  • 38
  • 46
  • i do need the new iOS features, unfortunately.. otherwise my rotation is totally boned on iOS 6. so, for now, I removed copy/paste from my app entirely. oy vey. – toblerpwn Sep 22 '12 at 19:36

3 Answers3

13

This works for me on Xcode 4.5 for my iOS 6 devices.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];        
NSData *imgData = UIImagePNGRepresentation(@"image");
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
rob1302
  • 1,631
  • 3
  • 15
  • 18
  • NICE. Works perfectly for me on both 5.1 and 6.0 - when copying one image. How about copying multiple images to clipboard, as in `UIPasteboard`'s `setImages:`? – toblerpwn Sep 27 '12 at 20:47
  • 1
    Using the setImages: still seems to not work. Guess we'll have to wait for Apple to figure out what's actually wrong with Xcode 4.5. – rob1302 Sep 28 '12 at 02:59
  • Works perfectly. Quickly solved a little panic when I noticed my apps weren't working in iOS6 for no reason! Strange that this functionality changed but we didn't get any warnings or anything about it. – Boeckm Nov 01 '12 at 23:19
  • works fine in ios 6.0.1! i was sending a JPEG so i change the array to load position number 1 – João Nunes Nov 16 '12 at 14:15
  • Don't use [UIPasteboardTypeListImage objectAtIndex:0] - its dangerous to assume that Apple will not change the order one day. The docs give nothing about the order. – Tom Andersen Jul 25 '13 at 15:07
  • What would you suggest using instead if Apple were to change the order without telling anyone? – rob1302 Jul 30 '13 at 23:47
7

For only one image, you should use:

#import <MobileCoreServices/UTCoreTypes.h>

For JPEG:

NSData *jpegData = UIImageJPEGRepresentation(image, 1.0);
[[UIPasteboard generalPasteboard] setData:jpegData forPasteboardType:(id)kUTTypeJPEG];

or For PNG:

NSData *pngData = UIImagePNGRepresentation(image);
[[UIPasteboard generalPasteboard] setData:pngData forPasteboardType:(id)kUTTypePNG];

and avoid indexing directly in UIPasteboardTypeListImage.

Francois Robert
  • 556
  • 10
  • 14
  • 1
    +1 for adding the #import, which others neglected to mention. – Rich Apodaca Mar 02 '13 at 01:04
  • you can also use @"public.jpeg" instead of kUTTTypeJPEG, as its a public constant that will never change. This saves you from linking in a whole library for one constant. – Tom Andersen Jul 25 '13 at 15:05
  • in iOS8 I was having trouble with pasting into Notes application but the code above worked. With the code I'm sharing below, pasting in Messages application was working, but now both work:UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; if (image) { [pasteBoard setImage:image]; } – C0D3 Oct 27 '14 at 17:46
1

I sent an email about this issue to Apple Developer Technical Support and I got this reply:

Thank you for contacting Apple Developer Technical Support. Our engineers have reviewed your request and have determined that this would be best handled as a bug report.

Please submit a complete bug report regarding this issue using the Bug Reporter tool at http://bugreport.apple.com.

So it is a bug for sure...

Community
  • 1
  • 1
iTarek
  • 736
  • 1
  • 8
  • 17
  • Good call; added bug 12408800 on Apple's site. Will report back with their response. – toblerpwn Oct 01 '12 at 21:52
  • I had also opened up a Apple Developer Technical Support with this problem and then I came up with the solution I posted the other day and sent it to them. So they know about the problem already and i've also created a bug report. Wonder if i'll get my Tech Support Incident back since I answered my own question for them ;) – rob1302 Oct 02 '12 at 19:50