将图像和文本复制到 UIPasteboard
我想将图像和文本(两者)复制到 UIPasteBoard。 是否可以同时复制文本和图像。
这里我可以仅复制图像或仅复制文本。 如何复制两者?
我复制图像的代码如下,
UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
pasteBoard.persistent = YES;
NSData *data = UIImagePNGRepresentation(newImage);
[pasteBoard setData:data forPasteboardType:(NSString *)kUTTypePNG];
提前致谢!!!!
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);
[pasteBoard setData:data forPasteboardType:(NSString *)kUTTypePNG];
Thanks in advance !!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我的代码,它在我的设备上完美运行。
Here is my code and it is working perfectly on my device.
您应该设置粘贴板的 items 属性 -
参考中的项目描述是 -
因此,您可以将两个字典添加到一个数组中,键值对为 &并将该数组设置为 items 属性。
You should be setting the items property of the pasteboard-
The description of items from the reference is-
So, you can add two dictionaries to an array, with key value pairs being & and set this array to the items property.
根据我的经验,官方的方法在iOS上根本行不通。不要为每个项目创建单独的字典并将其添加到数组中(如文档中所述),而是将所有项目添加到单个字典中,然后使用该单个字典创建一个数组并将其设置到粘贴板。
像这样:
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:
这个问题很久以前就被问到了,但它仍然具有相关性——特别是因为 Apple 文档并没有让 Swift 多格式 UIPasteboard API 变得非常清晰。一直在努力弄清楚如何进行多格式复制和粘贴,我想我会分享我的解决方案,以防它对其他人有帮助。就我而言,我需要支持内部格式(包含所有详细信息)以及用于粘贴到其他应用程序中的图像和文本版本。
首先,您需要访问 UTI 常量 - 如果不在文件顶部添加以下内容,您将获得未解析的符号:
import MobileCoreServices
然后定义您的 UTI 格式:
let my_private_uti = " com.mydomain.myapp.myformat"
这是示例多格式副本的代码(在我的例子中是音乐程序):
现在进行粘贴。我想接受我的内部格式(如果可用),如果不可用,则退回到处理文本。 (在我的例子中,不要对图形格式执行任何操作。)
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):
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.)
似乎在 iOS 中设置 Apple 的持久布尔值是没有意义的:
Seems like it's pointless to set the
persistent
boolean in iOS, from Apple: