在 iPhone 上复制并粘贴多种数据表示形式

发布于 2024-07-25 02:26:13 字数 681 浏览 3 评论 0原文

当我尝试将多个数据表示放到 iPhone 3.0 的粘贴板上时,遇到了一些问题。

我想做的是将数据表示形式和字符串表示形式放到粘贴板上。 数据是我自己的数据类型,我用它来复制和粘贴到我的应用程序中。 字符串表示形式是一种将应用程序内容作为大纲复制并粘贴到其他应用程序(例如 Mail.app)中的方法。

    // payload
NSString *pasteboardString = [selectedNode stringRepresentation];
NSDictionary *pasteboardDictionary = [selectedNode nodeAndSubnodesProperties];

// set payload
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = pasteboardString;
[pasteboard setValue:pasteboardDictionary forPasteboardType:MNTNodesPasteboardType];

上面的代码不起作用,因为字符串属性和 setValue:forPasteboardType: 方法替换了粘贴板上的第一个表示形式。 我尝试了 addItems: 但它对我不起作用。

感谢您的任何帮助!

I encountered some issues when trying to put more than one data representation onto the pasteboard on iPhone 3.0.

What I'm trying to do is put a data representation and a string representation onto the pasteboard. The data is my own data type and I use it for copy and paste in my application. The string representation is a way to copy and paste the content of my application as an outline into an other application (for example Mail.app).

    // payload
NSString *pasteboardString = [selectedNode stringRepresentation];
NSDictionary *pasteboardDictionary = [selectedNode nodeAndSubnodesProperties];

// set payload
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = pasteboardString;
[pasteboard setValue:pasteboardDictionary forPasteboardType:MNTNodesPasteboardType];

The above code doesn't work because the string property and setValue:forPasteboardType: methode replace the first representation on the pasteboard. I tried addItems: but it didn't work for me.

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-08-01 02:26:13

要回答我自己的问题:

您必须使用 items 属性将多个表示形式放置到粘贴板上。 为此,您需要创建一个字典,其中每个表示形式作为值,表示形式类型作为键。 将此字典添加到一个数组中,其中数组中的每个项目代表一个项目(UIPasteboard 支持将多个项目添加到粘贴板以及为每个项目添加多个表示)。

具有两种表示形式的单个项目的示例代码:

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:2];
[item setValue:[NSKeyedArchiver archivedDataWithRootObject:pasteboardDictionary] forKey:MNTNodesPasteboardType];
[item setValue:pasteboardString forKey:(NSString *)kUTTypeUTF8PlainText];
pasteboard.items = [NSArray arrayWithObject:item];

不要忘记链接 MobileCoreServices 框架来解析 UTI 常量。

To answer my own question:

You have to used the items property to put multiple representations onto the pasteboard. To do so you create a dictionary with each representation as the value and the representation type as the key. Add this dictionary to an array, where each item in the array represents an item (UIPasteboard supports adding multiple items to the pasteboard as well as adding mutliple representation to each item).

Example code for one single item with two representations:

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:2];
[item setValue:[NSKeyedArchiver archivedDataWithRootObject:pasteboardDictionary] forKey:MNTNodesPasteboardType];
[item setValue:pasteboardString forKey:(NSString *)kUTTypeUTF8PlainText];
pasteboard.items = [NSArray arrayWithObject:item];

Don't forget to link with the MobileCoreServices framework to resolve the UTI constant.

新一帅帅 2024-08-01 02:26:13

这就是 Swift 对我有用的东西
它将图像和文本一起粘贴到粘贴板上

let pastebaord = UIPasteboard.generalPasteboard()
let image = UIImage(named: "my-image-file")
pastebaord.addItems([ [UIPasteboardTypeListString[0] as! String : "hello"], [UIPasteboardTypeListImage[0] as! String: image!]]) 

this is what worked for me in Swift
it pastes both an image and text together to the pastboard

let pastebaord = UIPasteboard.generalPasteboard()
let image = UIImage(named: "my-image-file")
pastebaord.addItems([ [UIPasteboardTypeListString[0] as! String : "hello"], [UIPasteboardTypeListImage[0] as! String: image!]]) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文