将图像和文本复制到UIPasteboard

Vij*_*jay 4 iphone uipasteboard

我想将图像和文本(两者)复制到UIPasteBoard. 是否可以复制文本和图像.

在这里,我只能复制图像或仅复制文本.如何复制?

我的复制图像代码如下,

UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:NO];
pasteBoard.persistent = YES;
NSData *data = UIImagePNGRepresentation(newImage);
[pasteBoard setData:data forPasteboardType:(NSString *)kUTTypePNG]; 
Run Code Online (Sandbox Code Playgroud)

提前致谢 !!!!!

Sha*_*bal 11

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

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];
Run Code Online (Sandbox Code Playgroud)


Aks*_*hay 4

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

参考文献中的项目描述是-

项目

粘贴板上的粘贴板项目。@property(nonatomic,copy) NSArray *items 讨论

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

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