【问题标题】:How to copy UIImage data to clipboard/UIPasteboard? And how do I test it?如何将 UIImage 数据复制到剪贴板/UIPasteboard?我该如何测试它?
【发布时间】:2011-10-02 13:30:21
【问题描述】:

单击菜单中的“复制”项,我尝试使用以下代码将图像数据复制到UIPasteboard

UIPasteboard *gpBoard = [UIPasteboard generalPasteboard];
[[gpBoard setData:UIImageJPEGRepresentation(imgView.image, 1.0) forPasteboardType:UIPasteboardTypeListImage];

forPasteboardType:需要发送哪个参数,复制数据后如何测试?

【问题讨论】:

标签: ios cocoa-touch copy-paste uipasteboard


【解决方案1】:

这要容易得多:

[UIPasteboard generalPasteboard].image = imgView.image;

再次取出图像:

UIImage *image = [UIPasteboard generalPasteboard].image;

【讨论】:

  • 非常感谢,我该如何测试?我应该把图像拿出来并将其设置为某个 ImageView 吗?
  • @pradeepa 您可以打开 iMessage 或 whatsapp 或 facebook Messenger 并粘贴图像。如果你看到你复制的图像,那么它就完成了^_^
  • 我没有看到它被粘贴,我正在尝试将它粘贴到 UITextView,我需要为它设置什么吗?
【解决方案2】:

我尝试了上述方法,但由于某种原因,它在 iOS 7,8 上很受欢迎。有时有效,有时无效,即系统不显示“粘贴”选项。

这在所有设备和所有 iOS 上都对我有效

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:@"test.png"]);
    [pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];

【讨论】:

    【解决方案3】:

    斯威夫特:

    UIPasteboard.generalPasteboard().image = imageToCopy
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2012-10-16
      • 2012-11-06
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多