How can I get raw data of image from UIPasteboard if it's copied by another app (such as photos, safari, etc..)

StackOverflow https://stackoverflow.com/questions/19294331

Pregunta

Is there a way to get raw data of image from UIPasteboard instead of UIImage if it's copied by another app such as photos or mobile safari?

I'm currently facing wired differences between IOS 6.0 and IOS 6.1(7.0 also)

In IOS 6.0, UIPasteboard's item of the copied image by photos or mobile safari contains raw data of the image. But In IOS 6.1 and above, it contains UIImage instead of raw data.

In IOS 6.0, copied item of UIPasteboard is below

Printing description of array:
<__NSArrayM 0x8a804c0>(
{
    "com.compuserve.gif" = <47494638 39614002 ...... 3b3a2000 3b>;
    "public.url" = "url of the image....";
}
)

In IOS 6.1 and above, it contains UIImage instead of raw data.

Printing description of array:
<__NSArrayM 0xa25b7b0>(
{
    "com.compuserve.gif" = "<UIImage: 0x9429570>";
    "public.url" = "url of the image...";
}
)

If that image format is PNG or JPEG, it's not that bad. (I still have to compress again if it's JEPG though.) But when I try to paste animated gif image, it becomes more complicated.

I don't know even it's passible to create animated gif image from normal UIImage.

I can download again from original url, but downloading data that I already have seems not good solution I think. And also, if it's copied from photos app, there's no such url. (there's some mysterious uri named "com.apple.mobileslideshow.asset-object-id-uri" that is undocumented instead of url)

There seems a workaround, because when I try to do exactly same action between photos and email app, It works properly

Any suggestions?

¿Fue útil?

Solución

Well now, I figured it out myself.

You can simply get raw (binary)data of the image form general pasteboard by sending dataForPasteboardType:(NSSting*)PasteboardType message to general pasteboard, if it's copied from Apple's built-in Mobile Safari or Photos App. (@"com.compuserve.gif" for the pasteboard type in my case)

I myself feel a bit foolish for not having checked all the passible methods sooner. :(

My confusion comes from items property of the UIPasteboard. I thought that items are containing all of data of current pasteboard. So I try to save that array from pasteboard and want to use it later, but I were totally wrong.

As documented in UIPasteboard Class Reference, the items property contains dictionary with key being the representation type and the "value" the object associated with that type.

At this point of time, The "value" refers really "value" of the representation, not the data of that type. This meaning of the "value" is the same as the value of thesetValue: forPasteboardType: method.

On the other words, you cannot retrieve raw(binary) data of the image from items property, even if you set the image to the pasteboard by sending setData: forPasteboardType: message.(I tested it on IOS 7)

In addition, raw data of the image from items property in IOS 6.0 seems a bug of that OS version. (This may not true, it's just my opinion)

Otros consejos

You can get NSData from UIPasteboard if you specify right PasteboardType:

NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];

Do not forget to import

<MobileCoreServices/MobileCoreServices.h>

The UIPasteBoard will contain whatever is placed in it. It's up to the app that is copying to the paste board to put the contents in the proper format. The app can place items as raw binary data or as objects such as UIImage in the paste board.

If you're getting something different between iOS versions, you're probably using different versions of the app or it's simply copying things differently.

You're right that you can't represent an animated GIF in a UIImage because a UIImage only contains a single image. Perhaps the app is just copying the first frame's bitmap data in that case?

You can convert a UIImage to raw JPEG data using UIImageJPEGRepresentation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top