将图像和文本复制到 UIPasteboard

发布于 2024-12-08 15:26:27 字数 428 浏览 9 评论 0原文

我想将图像和文本(两者)复制到 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 技术交流群。

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

发布评论

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

评论(5

安穩 2024-12-15 15:26:27

这是我的代码,它在我的设备上完美运行。

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];

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];
祁梦 2024-12-15 15:26:27

您应该设置粘贴板的 items 属性 -

参考中的项目描述是 -

项目

粘贴板上的粘贴板项目。 @property(非原子,复制)
NSArray *items 讨论

该属性的值是一个字典数组。每本词典
代表一个粘贴板项目,关键是表示
数据对象或属性列表对象关联的类型和值
与那种类型。设置此属性将替换所有当前的
纸板项目。

因此,您可以将两个字典添加到一个数组中,键值对为 &并将该数组设置为 items 属性。

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.

臻嫒无言 2024-12-15 15:26:27

根据我的经验,官方的方法在iOS上根本行不通。不要为每个项目创建单独的字典并将其添加到数组中(如文档中所述),而是将所有项目添加到单个字典中,然后使用该单个字典创建一个数组并将其设置到粘贴板。

像这样:

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

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]];
国产ˉ祖宗 2024-12-15 15:26:27

这个问题很久以前就被问到了,但它仍然具有相关性——特别是因为 Apple 文档并没有让 Swift 多格式 UIPasteboard API 变得非常清晰。一直在努力弄清楚如何进行多格式复制和粘贴,我想我会分享我的解决方案,以防它对其他人有帮助。就我而言,我需要支持内部格式(包含所有详细信息)以及用于粘贴到其他应用程序中的图像和文本版本。

首先,您需要访问 UTI 常量 - 如果不在文件顶部添加以下内容,您将获得未解析的符号:

import MobileCoreServices

然后定义您的 UTI 格式:

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

这是示例多格式副本的代码(在我的例子中是音乐程序):

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)

现在进行粘贴。我想接受我的内部格式(如果可用),如果不可用,则退回到处理文本。 (在我的例子中,不要对图形格式执行任何操作。)

//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
        }
    }
}

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
        }
    }
}
云柯 2024-12-15 15:26:27

似乎在 iOS 中设置 Apple 的持久布尔值是没有意义的:

iOS,公共(系统)粘贴板是持久的,但默认情况下私有(应用程序)粘贴板不是。 当创建它们的应用程序退出时,这些私有粘贴板不会继续存在。但是,您可以将应用程序粘贴板标记为持久。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文