开发者

copy image and text to the UIPasteboard

I want to copy image and text (both) to UIPasteBoard. Is it possible to copy both the text and image.

Here I can copy image only or text only . How to copy both ?

My code for copy image is as follows,

UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
pasteBoard.persistent = YES;
NSData *data = UIImagePNGRepresentation(newImage);
[past开发者_如何学PythoneBoard setData:data forPasteboardType:(NSString *)kUTTypePNG]; 

Thanks in advance !!!!!


Here is my code and it is working perfectly on my device.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = NO;

NSMutableDictionary *text = [NSMutableDictionary dictionaryWithCapacity:1];
[text setValue:captionLabel.text forKey:(NSString *)kUTTypeUTF8PlainText];

NSMutableDictionary *image = [NSMutableDictionary dictionaryWithCapacity:1];
[image setValue:gratitudeImageView.image forKey:(NSString *)kUTTypePNG];

pasteboard.items = [NSArray arrayWithObjects:image,text, nil];


You should be setting the items property of the pasteboard-

The description of items from the reference is-

items

The pasteboard items on the pasteboard. @property(nonatomic,copy) NSArray *items Discussion

The value of the property is an array of dictionaries. Each dictionary represents a pasteboard item, with the key being the representation type and the value the data object or property-list object associated with that type. Setting this property replaces all of the current pasteboard items.

So, you can add two dictionaries to an array, with key value pairs being & and set this array to the items property.


In my experience, the official way simply does not work in iOS. Instead of creating an individual dictionary for each item and adding those to the array (as stated in the documentation), add all items to a single dictionary, then make an array with that single dictionary and set that to the pasteboard.

Like this:

NSMutableDictionary * pasteboardDict = [NSMutableDictionary dictionary];
[pasteboardDict setObject:someData forKey:someUTIkey];
[pasteboardDict setObject:someOtherData forKey:someOtherUTIkey];
[[UIPasteboard generalPasteboard]setItems:[NSArray arrayWithObject:pasteboardDict]];


This question was asked a long time ago, but it's still relevant - and especially since Apple docs don't make Swift multi-format UIPasteboard APIs very clear. Having struggled to figure out how to do multiple-format copy & paste, I thought I'd share my solution in case it helped anyone else. In my case, I needed to support an internal format (containing all the particulars), as well as image and text versions for pasting into other apps.

First, you need to get access to the UTI constants - you'll get unresolved symbols without adding this at the top of your file:

import MobileCoreServices

Then define your format UTI:

let my_private_uti = "com.mydomain.myapp.myformat"

Here's the code for an example multi-format copy (in my case for a music program):

externalRepresentation = "[A7]"
internalRepresentation = "A7:0 0 2 0 2 0"
image = UIImage()            
// fill image with chord diagram...

let pasteboard =
    [ [kUTTypeUTF8PlainText as String : externalRepresentation],
      [kUTTypePNG as String: UIImagePNGRepresentation(image!)!],
      [my_private_uti: internalRepresentation]]

    UIPasteboard.general.setItems(pasteboard)

And now for the the paste. I want to accept my internal format if it's available, and fall back to processing text if it's not. (Don't do anything with a graphical format in my case.)

//Handle internal format
if let pastedata = UIPasteboard.general.data(forPasteboardType:my_private_uti, inItemSet:nil)  {
    if pastedata.count > 0 {
        if let ourformat = String(data: pastedata[0] as! Data, encoding: .utf8) {
        // Process ourformat string
        print("Pasted internal representation: \(ourformat)")
        return
        }
    }
}

// Handle plain text format 
if let pastedata = UIPasteboard.general.data(forPasteboardType:kUTTypeUTF8PlainText as String, inItemSet:nil) {
if pastedata.count > 0 {
    if let textformat = String(data: pastedata[0] as! Data, encoding: .utf8) {
        // Process normal text
        print("Pasted external representation: \(textformat)")
        return
        }
    }
}


Seems like it's pointless to set the persistent boolean in iOS, from Apple:

iOS, public (system) pasteboards are persistent, but by default private (application) pasteboards are not. These private pasteboards do not continue to exist when the application that creates them quits. However, you can mark application pasteboards as persistent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜