FMX TClipboard now supports iOS

I’ve just checked in a revision of my open source FMX TClipboard implementation that has an iOS backend. This supports the current rather than the FPC-based version of ‘Delphi for iOS’, however the Windows and OS X backends still compile with XE2 and above.

In essence, the new code wraps the native iOS clipboard API (UIPasteboard) and presents it in a fashion that closely follows the VCL clipboard interface, just like the existing Windows and OS X support did the same for desktop platforms. For some reason Apple in their wisdom decided to make the iOS clipboard API similar yet randomly different to the OS X one, so even though there’s not masses of code, it was a bit fiddly to implement. Anyhow, I’ve also knocked out a little demo similar to the previous desktop one:

TiOSClipboard demo

Using (say) Photos, you can copy an image to the clipboard and paste it into the demo. Conversely, you can from the demo itself copy either just the text entered, just the image, both the text and the image as two representations of the same clipboard item, or both the text and image as a custom clipboard format. The ‘List Formats on Clipboard’ button, as its name implies, then lists the formats currently on the clipboard. This is what I get after copying an image from Photos on the iOS simulator:

Format List

Technically, the iOS clipboard, like the OS X one, can have multiple items, each with multiple representations. Since the multiple-item concept doesn’t exist on Windows (indeed, it didn’t exist on OS X originally either), my class is only concerned with the first item, which is what most applications only bother with anyhow.

If you want the code, the SVN URL for it and a few other pieces is the following:

The core files are now CCR.FMXClipboard.pas, CCR.FMXClipboard.Apple.pasCCR.FMXClipboard.iOS.pas, CCR.FMXClipboard.Mac.pas and CCR.FMXClipboard.Win.pas, and together they stand alone.

9 thoughts on “FMX TClipboard now supports iOS

  1. Hi Chris,
    amazing work! Thanks!
    Two minor issues:
    – DPR file references not existing CCR.FMXClipboard.Android.pas
    – When pasting a copied JPEG image (“public.jpeg”) from photo album i get a black image in the app. My device is IPhone 4 with IOS 6.1

    Anywhere, good work and nice cross platform unit.

    • DPR file references not existing CCR.FMXClipboard.Android.pas

      Eek, you’re right! Now fixed, thanks.

      When pasting a copied JPEG image (“public.jpeg”) from photo album i get a black image in the app. My device is IPhone 4 with IOS 6.1

      Hmm, is that all the time, or only with some images?

  2. Hi Chris,
    black image for every image in photo-album.
    I’ve disabled the GPU canvas with
    TPlatformServices.Current.GlobalFlags.Add(GlobalDisableiOSGPUCanvas, True);
    before Application.Initialize. Then it works but the bitmap is flipped vertical. I hope this helps,
    Best regards Dirk

    • Thanks. Does this happen with small images as well (< 1024 pixels square)? If not, I’ve just made a small revision that enforces a maximum pasted bitmap size on iOS…

  3. Yes, that did the trick. Org. size was 1936×2592 and pasted as black image. Then i reduced size to 596×635 and pasting works fine.
    Can you explain what’s going on? too much memory consumption?

    • The Apple docs for the UIImage class (= the native Cocoa Touch equivalent to TBitmap) say

      You should avoid creating UIImage objects that are greater than 1024 x 1024 in size.

      This gives rise to the question of how a ‘large’ image can get copied to the clipboard in the first place. My guess (and it’s only a guess) is that when Photos puts a UIImage on the clipboard, the UIImage only stores a file name internally, rather than the actual bitmap bits. In contrast, TBitmap always works in terms of actual bitmap bits, which means code needs to explicitly downsample if necessary (and revision 89 does exactly that).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s