iOS: UIPasteboard setImage: fails on iOS6 and/or X

2019-03-16 03:32发布

问题:

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..):

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

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?

回答1:

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]];


回答2:

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.



回答3:

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...